Skip to content

Commit e844cc6

Browse files
committed
Adding Accepted Solution - Problem - 18 - H - index II
1 parent ba6b7b1 commit e844cc6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##==================================
2+
## Leetcode June Challenge
3+
## Username: Vanditg
4+
## Year: 2020
5+
## Problem: 18
6+
## Problem Name: H-Index II
7+
##===================================
8+
#Given an array of citations sorted in ascending order (each citation is a non-negative integer)
9+
#of a researcher, write a function to compute the researcher's h-index.
10+
#
11+
#According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers
12+
#have at least h citations each, and the other N − h papers have no more than h citations each."
13+
#
14+
#Example:
15+
#
16+
#Input: citations = [0,1,3,5,6]
17+
#Output: 3
18+
#Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
19+
# received 0, 1, 3, 5, 6 citations respectively.
20+
# Since the researcher has 3 papers with at least 3 citations each and the remaining
21+
# two with no more than 3 citations each, her h-index is 3.
22+
class Solution:
23+
def hIndex(self, citations):
24+
n = len(citations)
25+
left, right = 0, n - 1
26+
while left <= right:
27+
mid = int((left + right) / 2)
28+
if citations[mid] >= n - mid:
29+
right = mid - 1
30+
else:
31+
left = mid + 1
32+
return n - left

0 commit comments

Comments
 (0)