Skip to content

Commit e87a2ac

Browse files
committed
274_H-Index
322_Coin_Change
1 parent 0d01775 commit e87a2ac

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@
8686
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/259_3Sum_Smaller.py) | 1. Reduce to two sum smaller, then binary search, O(n^2lgn) and O(1)<br>2. Reduce to two sum smaller, then two points, O(n^2) and O(1)|
8787
| 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/266_Palindrome_Permutation.py) | Compute frequency, check number of odd occurrences <= 1 then palindrome, O(n) and O(n)|
8888
| 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/267_Palindrome_Permutation_II.py) | Check palindrome then generate half with [Permutations II](https://leetcode.com/problems/permutations-ii/), O(n^2) and O(n^2) |
89+
| 274 | [H-Index](https://leetcode.com/problems/h-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/274_H-Index.py) | [Background](https://en.wikipedia.org/wiki/H-index)<br>1. Sort and check number of papers greater than h, O(nlogn) and O(1)<br>2. Counting sort, O(n) and O(n) |
8990
| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
9091
| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/305_Number_of_Islands_II.py) | Quick union find with weights, O(nlogn) and O(n) |
92+
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/322_Coin_Change.py) | Bottom-up or top-down DP, dp[n] = min(dp[n], dp[n - v_i]), where v_i is the coin, O(amount * n) and O(amount) |
9193
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
9294
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
9395
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

python/274_H-Index.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
# def hIndex(self, citations):
3+
# """
4+
# :type citations: List[int]
5+
# :rtype: int
6+
# """
7+
# # Sort and check the max h where the number of paper with no less than h citations
8+
# # is no less than h
9+
# citations.sort()
10+
# ls = len(citations)
11+
# h = ls
12+
# while h > 0 and citations[ls - h] < h:
13+
# h -= 1
14+
# return h
15+
16+
# def hIndex(self, citations):
17+
# citations.sort()
18+
# i = 0
19+
# while i < len(citations) and citations[len(citations) - 1 - i] > i:
20+
# i += 1
21+
# return i
22+
23+
def hIndex(self, citations):
24+
# counting sort
25+
ls = len(citations)
26+
papers = [0] * (ls + 1)
27+
for c in citations:
28+
papers[min(ls, c)] += 1
29+
k, s = ls, papers[ls]
30+
while k > s:
31+
k -= 1
32+
s += papers[k]
33+
return k

python/322_Coin_Change.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution(object):
2+
# def coinChange(self, coins, amount):
3+
# """
4+
# :type coins: List[int]
5+
# :type amount: int
6+
# :rtype: int
7+
# """
8+
# # Top-down dp
9+
# if amount < 1:
10+
# return 0
11+
# return self.coinChange_helper(coins, amount, [0] * (amount + 1))
12+
13+
# def coinChange_helper(self, coins, amount, count):
14+
# if amount < 0:
15+
# return -1
16+
# if amount == 0:
17+
# return 0
18+
# if count[amount - 1] != 0:
19+
# return count[amount - 1]
20+
# min_count = 1000000000
21+
# for coin in coins:
22+
# res = self.coinChange_helper(coins, amount - coin, count)
23+
# if res >= 0 and res < min_count:
24+
# min_count = 1 + res
25+
# if min_count == 1000000000:
26+
# count[amount - 1] = -1
27+
# else:
28+
# count[amount - 1] = min_count
29+
# return count[amount - 1]
30+
31+
def coinChange(self, coins, amount):
32+
# bottom-up dp
33+
if amount == 0:
34+
return 0
35+
if coins is None or len(coins) == 0:
36+
return -1
37+
coins.sort()
38+
dp = [1000000000] * (amount + 1)
39+
for i in range(1, amount + 1):
40+
for coin in coins:
41+
if i == coin:
42+
dp[i] = 1
43+
break
44+
elif i > coin:
45+
dp[i] = min(dp[i], dp[i - coin] + 1)
46+
if dp[amount] == 1000000000:
47+
return -1
48+
return dp[amount]

0 commit comments

Comments
 (0)