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:
parent
ad8b800a0b
commit
1183f0e046
1 changed files with 19 additions and 0 deletions
19
go/h-index.go
Normal file
19
go/h-index.go
Normal 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")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue