-
Notifications
You must be signed in to change notification settings - Fork 43
/
h-index-ii.py
153 lines (138 loc) · 4.03 KB
/
h-index-ii.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Follow up for H-Index: (274)
What if the citations array is sorted
in ascending order? Could you optimize your algorithm?
Hint:
Expected runtime complexity is in O(log n) and the input is sorted.
ref :
https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Sort/h-index.py
"""
# V0
# IDEA : BINARY SEARCH
class Solution:
def hIndex(self, c):
# NOTE : right = len(c) -1
left, right = 0, len(c)-1
res = 0
while left <= right:
mid = left + (right-left)//2
rem = len(c) - mid # NOTE this
if c[mid] >= rem:
res = rem
right = mid - 1 # NOTE this, we want mid become smaller
else:
left = mid + 1 # NOTE this, we want mid become bigger
return res
# V0'
# IDEA : SAME AS #274 H-Index
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
# reverse ordeing the citations first, so the first validated case is the max h-index
# i : how many digit in the list that are >= c
# c : the value of "possible" h-index
for i, c in enumerate(sorted(citations, reverse = True)):
if i >= c:
return i
# BE AWARE OF IT
# for the [] or [1] ... cases
return len(citations)
# V1
# https://leetcode.com/problems/h-index-ii/discuss/709447/Python-O(logN)-Binary-Search
# IDEA : BINARY SEARCH
class Solution:
def hIndex(self, c: List[int]) -> int:
left, right = 0, len(c)-1
res = 0
while left <= right:
mid = left + (right-left)//2
rem = len(c) - mid
if c[mid] >= rem:
res = rem
right = mid - 1
else:
left = mid + 1
return res
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82949743
# IDEA : when a scientist has index = h, then there are at least h citations of all his
# papers.
# h=0 -> at least 0 citations paper
# h=1 -> at least 1 citations paper
# ....
# h=6 -> at least 6 citations papers
# ...
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
N = len(citations)
l, r = 0, N - 1
H = 0
while l <= r:
mid = l + (r - l) / 2
H = max(H, min(citations[mid], N - mid))
if citations[mid] < N - mid:
l = mid + 1
else:
r = mid - 1
return H
# V1'
# http://bookshadow.com/weblog/2015/09/04/leetcode-h-index-ii/
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
N = len(citations)
low, high = 0, N - 1
while low <= high:
mid = (low + high) / 2
if N - mid > citations[mid]:
low = mid + 1
else:
high = mid - 1
return N - low
# V1''
# https://www.hrwhisper.me/leetcode-h-index-h-index-ii/
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
return max([min(i + 1, c) for i, c in enumerate(sorted(citations, reverse=True))])
# V1'''
# https://www.hrwhisper.me/leetcode-h-index-h-index-ii/
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
return sum([i < c for i, c in enumerate(sorted(citations, reverse=True))])
# V2
# Time: O(logn)
# Space: O(1)
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
n = len(citations)
left, right = 0, n - 1
while left <= right:
mid = (left + right) / 2
if citations[mid] >= n - mid:
right = mid - 1
else:
left = mid + 1
return n - left