forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path274. H-Index.go
58 lines (54 loc) · 934 Bytes
/
274. H-Index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package leetcode
// 解法一
func hIndex(citations []int) int {
n := len(citations)
buckets := make([]int, n+1)
for _, c := range citations {
if c >= n {
buckets[n]++
} else {
buckets[c]++
}
}
count := 0
for i := n; i >= 0; i-- {
count += buckets[i]
if count >= i {
return i
}
}
return 0
}
// 解法二
func hIndex1(citations []int) int {
quickSort164(citations, 0, len(citations)-1)
hIndex := 0
for i := len(citations) - 1; i >= 0; i-- {
if citations[i] >= len(citations)-i {
hIndex++
} else {
break
}
}
return hIndex
}
func quickSort164(a []int, lo, hi int) {
if lo >= hi {
return
}
p := partition164(a, lo, hi)
quickSort164(a, lo, p-1)
quickSort164(a, p+1, hi)
}
func partition164(a []int, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
if a[j] < pivot {
i++
a[j], a[i] = a[i], a[j]
}
}
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}