go: add «274. H-Index»

URL:	https://leetcode.com/problems/h-index/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-04 20:35:31 +01:00
parent ad8b800a0b
commit 1183f0e046
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

19
go/h-index.go Normal file
View file

@ -0,0 +1,19 @@
package main
func hIndex(citations []int) int {
counters := make([]int, len(citations)+1)
for _, citated := range citations {
counters[min(len(citations), citated)]++
}
total := 0
for i := len(citations); i >= 0; i-- {
total += counters[i]
if total >= i {
return i
}
}
panic("at least one counter should always match")
}