275.h-index-ii.go 336 B

1234567891011121314151617
  1. func hIndex(citations []int) int {
  2. // Target: find the first h that h <= citations[n-h]
  3. n := len(citations)
  4. beg, end := 0, n
  5. for beg < end {
  6. mid := beg + (end-beg)/2
  7. h, citation := n-mid, citations[mid]
  8. if h < citation {
  9. end = mid
  10. } else if citation < h {
  11. beg = mid + 1
  12. } else {
  13. return h
  14. }
  15. }
  16. return n - beg
  17. }