-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path274_H-Index.py
44 lines (39 loc) · 1.01 KB
/
274_H-Index.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
class Solution(object):
# An easy approach which sorts the array first.
def hIndex(self, citations):
if not citations:
return 0
citations.sort()
length = len(citations)
for i in range(length, 0, -1):
if citations[length-i] >= i:
return i
return 0
class Solution_2(object):
# A faster approach use extra space.
def hIndex(self, citations):
length = len(citations)
count = [0] * (length + 1)
for i in citations:
if i >= length:
count[length] += 1
else:
count[i] += 1
occur = 0
# Dynamic programming here to
# Sum the occuring times of citations bigger than one given value
for i in range(length, 0, -1):
occur += count[i]
if occur >= i:
return i
return 0
"""
[]
[0]
[23]
[4,4,4,4]
[4, 0, 6, 1, 5]
"""