https://leetcode.cn/problems/h-index/?envType=study-plan-v2&envId=top-interview-150

go

package leetcode150

import (
    "sort"
    "testing"
)

func TestHindex(t *testing.T) {
    nums := []int{3, 0, 6, 1, 5}
    res := hIndex(nums)
    println(res)
    for _, num := range nums {
        print(num)
        print(" ")
    }
}

func hIndex(citations []int) int {
    if len(citations) == 0 {
        return 0
    }
    sort.Sort(sort.Reverse(sort.IntSlice(citations)))
    total := 0
    for i := 0; i < len(citations); i++ {
        if citations[i] >= total+1 {
            total++
        } else {
            break
        }
    }
    return total
}