Skip to content

Commit ff831e9

Browse files
committed
update 235
1 parent bccf20a commit ff831e9

File tree

6 files changed

+106
-18
lines changed

6 files changed

+106
-18
lines changed

BreadthFirstSearch/322_CoinChange.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
# Refer to:
66
# https://leetcode.com/discuss/79289/fast-python-branch-bound-solution-beaten-python-submissions
77

8+
89
class Solution(object):
910
def coinChange(self, coins, amount):
1011
"""
1112
Very classic dynamic programming problem, like 0-1 Knapsack problem.
1213
dp[i] is the fewest number of coins making up amount i,
1314
then for every coin in coins, dp[i] = min(dp[i - coin] + 1).
1415
"""
15-
dp = [amount + 1] * (amount+1)
16+
dp = [amount + 1] * (amount + 1)
1617
dp[0] = 0
17-
for i in xrange(amount+1):
18+
for i in xrange(amount + 1):
1819
for coin in coins:
1920
if coin <= i:
20-
dp[i] = min(dp[i], dp[i-coin]+1)
21+
dp[i] = min(dp[i], dp[i - coin] + 1)
2122
return -1 if dp[amount] > amount else dp[amount]
2223

2324

@@ -31,7 +32,7 @@ def coinChange(self, coins, amount):
3132
count = 0
3233

3334
# upper bound on number of coins (+1 to represent the impossible case)
34-
coins.sort(reverse = True)
35+
coins.sort(reverse=True)
3536
upperBound = amount / coins[-1] + 1
3637

3738
# Use upperBound to pruning.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-10-09 12:06:21
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
dp[i][j] is the number of ways to remove some characters
10+
from S[0,i] to get T[0,j], we have the recursive formula:
11+
dp [i][j] = dp[i-1][j] if S[i] != T[j] ,
12+
or dp [i][j] = dp[i-1][j] + dp[i-1][j-1] if S[i] ==T[j]
13+
*/
14+
int numDistinct(string s, string t) {
15+
if(s=="" || t==""){
16+
return 0;
17+
}
18+
19+
int s_len = s.size();
20+
int t_len = t.size();
21+
if(s_len == t_len && s != t || (s_len < t_len)){
22+
return 0;
23+
}
24+
25+
/*
26+
dp[i][j] refer to only dp[i-1][j] and dp[i-1][j-1].
27+
This gives us the idea that we can reduce the space to O(n).
28+
Since we need to make use of dp[i-1][j-1], we run backward!!!
29+
*/
30+
std::vector<int> dp(t_len+1, 0);
31+
dp[0]=1;
32+
for(int i=1; i<s_len+1; i++){
33+
for(int j=t_len; j>0; j--){
34+
if(s[i-1] == t[j-1]){
35+
dp[j] += dp[j-1];
36+
}
37+
}
38+
}
39+
40+
return dp[t_len];
41+
}
42+
};
43+
44+
/*
45+
""
46+
"a"
47+
"ababcc"
48+
"abc"
49+
"rabbbit"
50+
"rabbit"
51+
*/

DynamicProgramming/115_DistinctSubsequences.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ def numDistinct(self, s, t):
1010
dp [i][j] = dp[i-1][j] if S[i] != T[j] ,
1111
or dp [i][j] = dp[i-1][j] + dp[i-1][j-1] if S[i] ==T[j]
1212
"""
13-
s_l = len(s)
14-
t_l = len(t)
15-
if s_l < t_l:
13+
if not s or not t:
1614
return 0
17-
if s_l == t_l and s != t:
18-
return 0
19-
if not s and t:
15+
16+
s_l, t_l = len(s), len(t)
17+
if s_l < t_l or (s_l == t_l and s != t):
2018
return 0
21-
if not t:
22-
return 1
2319

24-
dp = [0 for i in range(t_l+1)]
20+
dp = [0 for i in range(t_l + 1)]
2521
dp[0] = 1
2622

27-
for i in xrange(1, s_l+1):
23+
for i in xrange(1, s_l + 1):
2824
# dp[i][j] refer to only dp[i-1][j] and dp[i-1][j-1].
2925
# This gives us the idea that we can reduce the space to O(n).
3026
# Since we need to make use of dp[i-1][j-1], we run backward!!!
3127
for j in xrange(t_l, 0, -1):
32-
if s[i-1] == t[j-1]:
33-
dp[j] += dp[j-1]
28+
if s[i - 1] == t[j - 1]:
29+
dp[j] += dp[j - 1]
3430
return dp[t_l]
3531

3632
"""Readable, but take O(m*n) space
@@ -47,9 +43,12 @@ def numDistinct(self, s, t):
4743
return dp[s_l][t_l]
4844
"""
4945

46+
5047
"""
5148
""
5249
"a"
5350
"ababcc"
5451
"abc"
52+
"rabbbit"
53+
"rabbit"
5554
"""

DynamicProgramming/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@
8484
1. 需要设计数据结构来完成自底向上的计算过程。逻辑上相对不那么直观。
8585
2. 常常可以进行空间复杂度的优化。
8686

87-
88-
8987
# 例子:更好的理解
9088

9189
## [44. Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)
@@ -102,6 +100,11 @@
102100
121, 122, 123, 188, 309
103101

104102

103+
# 优化
104+
105+
115.Distinct Subsequences
106+
107+
105108
# 更多阅读
106109
[fibonacci数列为什么那么重要](https://www.zhihu.com/question/28062458)
107110
[Dynamic Programming:From Novice to Advanced](https://www.topcoder.com/community/data-science/data-science-tutorials/dynamic-programming-from-novice-to-advanced/)

Greedy/README.md

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-09-20 19:37:23
4+
*/
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* struct TreeNode {
9+
* int val;
10+
* TreeNode *left;
11+
* TreeNode *right;
12+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
// Easy to understand
18+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
19+
int min = p->val < q->val ? p->val : q->val;
20+
int max = p->val < q->val ? q->val : p->val;
21+
while(root){
22+
if(root->val < min){
23+
root = root->right;
24+
}
25+
else if(root->val > max){
26+
root = root->left;
27+
}
28+
else{
29+
return root;
30+
}
31+
}
32+
return nullptr;
33+
}
34+
};

0 commit comments

Comments
 (0)