lc274

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
N = len(citations)
d = []
t = 0
for x in range(N+1):
d.append(0)
for x in citations:
if x <= N:
d[x] += 1
else:
t += 1
for x in range(N+1):
xx = N - x
t += d[xx]
if t >= xx:
return xx
return -1
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
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
N = len(citations)
if N == 0:
return 0
citations.sort(reverse = True)
mid = N / 2
begin = 0
end = N - 1
while True:
if begin >= end:
if citations[mid] >= mid + 1:
return mid + 1
else:
return mid
if citations[mid] < mid + 1:
end = mid - 1
mid = (begin + end) / 2
elif citations[mid] > mid + 1:
begin = mid + 1
mid = (begin + end) / 2
else:
return mid + 1