Skip to content

Commit eb92959

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with conbination tag.
1 parent 961a315 commit eb92959

8 files changed

+236
-7
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@
500500
1. [Tag table from huahua](https://docs.google.com/spreadsheets/d/1SbpY-04Cz8EWw3A_LBUmDEXKUMO31DBjfeMoA0dlfIA/edit#gid=126913158)
501501

502502
### Advance
503-
#### [Trie Tree]()
503+
#### Trie Tree
504504
* [208. Implement Trie (Prefix Tree)](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/208.%20Implement%20Trie%20(Prefix%20Tree).md)
505505
* [648. Replace Words](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/648.%20Replace%20Words.md)
506506
* [676. Implement Magic Dictionary](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/676.%20Implement%20Magic%20Dictionary.md)
@@ -509,6 +509,28 @@
509509
* [745. Prefix and Suffix Search](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/745.%20Prefix%20and%20Suffix%20Search.md)
510510
* Conclusion: [Data structure | Trie Tree 字典树](https://seanforfun.github.io/datastructure/2018/11/07/TrieTree.html)
511511

512+
#### BIT / Segment tree
513+
* [307. Range Sum Query - Mutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/307.%20Range%20Sum%20Query%20-%20Mutable.md)
514+
* Conclusion: [Data structure | Binary Index Tree](https://seanforfun.github.io/datastructure/2018/03/03/BinaryIndexTree.html)
515+
* Conclusion: [Data structure | Segment Tree](https://seanforfun.github.io/datastructure/2018/03/04/SegmentTree.html)
516+
517+
#### Monotic stack
518+
* [84. Largest Rectangle in Histogram](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/84.%20Largest%20Rectangle%20in%20Histogram.md)
519+
* [85. Maximal Rectangle](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/85.%20Maximal%20Rectangle.md)
520+
* [901. Online Stock Span](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/901.%20Online%20Stock%20Span.md)
521+
* [907. Sum of Subarray Minimums](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/907.%20Sum%20of%20Subarray%20Minimums.md)
522+
523+
### Search
524+
#### Combination
525+
* [17. Letter Combinations of a Phone Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/17.%20Letter%20Combinations%20of%20a%20Phone%20Number.md)
526+
* [39. Combination Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/39.%20Combination%20Sum.md)
527+
* [40. Combination Sum II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/40.%20Combination%20Sum%20II.md)
528+
* [77. Combinations](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/77.%20Combinations.md)
529+
* [79. Word Search](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/79.%20Word%20Search.md)
530+
* [90. Subsets II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/90.%20Subsets%20II.md)
531+
* [216. Combination Sum III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/216.%20Combination%20Sum%20III.md)
532+
* Conclusion: We can speed up the recursion using an index and if we want all elements used once, we need to sort first and in the for loop, we need to remove duplicate values.
533+
512534

513535
## Algorithm(4th_Edition)
514536
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/17. Letter Combinations of a Phone Number.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Solution {
2222
if(null != digits && digits.length() == 0) return result;
2323
result.add("");
2424
String[] d = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
25-
25+
2626
int len = digits.length();
2727
for(int i = 0; i < len; i++){
2828
List<String> tempResult = new LinkedList<>();
@@ -65,4 +65,29 @@ class Solution {
6565
}
6666
}
6767
}
68-
```
68+
```
69+
70+
### Third time
71+
```Java
72+
class Solution {
73+
public List<String> letterCombinations(String digits) {
74+
String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
75+
List<String> res = new LinkedList<>();
76+
if(digits == null || digits.length() == 0) return res;
77+
dfs(digits, 0, res, letters, new StringBuilder());
78+
return res;
79+
}
80+
private void dfs(String digits, int index, List<String> result, String[] letters, StringBuilder sb){
81+
if(index >= digits.length()){
82+
result.add(sb.toString());
83+
return;
84+
}
85+
int i = digits.charAt(index) - '0';
86+
for(int j = 0; j < letters[i].length(); j++){
87+
sb.append(letters[i].charAt(j));
88+
dfs(digits, index + 1, result, letters, sb);
89+
sb.deleteCharAt(index);
90+
}
91+
}
92+
}
93+
```

leetcode/216. Combination Sum III.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Solution {
6262
backtrace(k, n, temp, result, 0, 1);
6363
return result;
6464
}
65-
65+
6666
private void backtrace(int k, int n, List<Integer> temp, List<List<Integer>> result, int sum, int cur){
6767
if(temp.size() == k){
6868
if(sum == n){
@@ -78,4 +78,26 @@ class Solution {
7878
}
7979
}
8080
}
81-
```
81+
```
82+
83+
### Third time
84+
```Java
85+
class Solution {
86+
public List<List<Integer>> combinationSum3(int k, int n) {
87+
List<List<Integer>> result = new LinkedList<>();
88+
dfs(result, n, k, 0, new LinkedList<Integer>(), 1);
89+
return result;
90+
}
91+
private void dfs(List<List<Integer>> result, int n, int k, int sum, List<Integer> temp, int index){
92+
if(sum == n && temp.size() == k) result.add(new LinkedList<Integer>(temp));
93+
else if(sum < n && temp.size() < k){
94+
for(int i = index; i <= 9; i++){
95+
if(i + sum > n) break;
96+
temp.add(i);
97+
dfs(result, n, k, sum + i, temp, i + 1);
98+
temp.remove(temp.size() - 1);
99+
}
100+
}
101+
}
102+
}
103+
```

leetcode/39. Combination Sum.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,28 @@ class Solution {
8585
}
8686
}
8787
}
88-
```
88+
```
89+
90+
### Third time
91+
```Java
92+
class Solution {
93+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
94+
List<List<Integer>> result = new LinkedList<>();
95+
if(candidates == null || candidates.length == 0) return result;
96+
dfs(result, candidates, 0, target, new LinkedList<>(), 0);
97+
return result;
98+
}
99+
public void dfs(List<List<Integer>> result, int[] candidates, int index, int target, List<Integer> temp, int sum){
100+
if(target == sum){
101+
result.add(new LinkedList<Integer>(temp));
102+
}else if(sum < target){
103+
for(int i = index; i < candidates.length; i++){
104+
if(sum + candidates[i] > target) continue;
105+
temp.add(candidates[i]);
106+
dfs(result, candidates, i, target, temp, sum + candidates[i]);
107+
temp.remove(temp.size() - 1);
108+
}
109+
}
110+
}
111+
}
112+
```

leetcode/40. Combination Sum II.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,30 @@ class Solution {
9696
}
9797
}
9898
}
99-
```
99+
```
100+
101+
### Third time
102+
```Java
103+
class Solution {
104+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
105+
List<List<Integer>> result = new LinkedList<>();
106+
if(candidates == null || candidates.length == 0) return result;
107+
Arrays.sort(candidates);
108+
dfs(result, candidates, target, 0, new LinkedList<>(), 0);
109+
return result;
110+
}
111+
private void dfs(List<List<Integer>> result, int[] candidates, int target, int sum, List<Integer> temp, int index){
112+
if(sum == target) result.add(new LinkedList<Integer>(temp));
113+
else if(sum < target){
114+
for(int i = index; i < candidates.length; i++){
115+
if(sum + candidates[i] > target) continue;
116+
while(i < candidates.length && i > index && candidates[i - 1] == candidates[i]) i++;
117+
if(i >= candidates.length) break;
118+
temp.add(candidates[i]);
119+
dfs(result, candidates, target, sum + candidates[i], temp, i + 1);
120+
temp.remove(temp.size() - 1);
121+
}
122+
}
123+
}
124+
}
125+
```

leetcode/77. Combinations.md

+23
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,26 @@ class Solution {
6666
}
6767
}
6868
```
69+
70+
### Third time
71+
```Java
72+
class Solution {
73+
public List<List<Integer>> combine(int n, int k) {
74+
List<List<Integer>> result = new LinkedList<>();
75+
if(n < 1 || n < k) return result;
76+
dfs(result, k, n, new LinkedList<Integer>(), 1);
77+
return result;
78+
}
79+
private void dfs(List<List<Integer>> result, int k, int n, List<Integer> temp, int index){
80+
if(k == 0){
81+
result.add(new LinkedList<Integer>(temp));
82+
}else if(k > 0){
83+
for(int i = index; i <= n; i++){
84+
temp.add(i);
85+
dfs(result, k - 1, n, temp, i + 1);
86+
temp.remove(temp.size() - 1);
87+
}
88+
}
89+
}
90+
}
91+
```

leetcode/78. Subsets.md

+21
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ class Solution {
8181
}
8282
}
8383
```
84+
85+
### Third time
86+
* Method 1: dfs
87+
```Java
88+
class Solution {
89+
public List<List<Integer>> subsets(int[] nums) {
90+
List<List<Integer>> result = new LinkedList<>();
91+
if(nums.length == 0) return result;
92+
dfs(result, nums, new LinkedList<Integer>(), 0);
93+
return result;
94+
}
95+
private void dfs(List<List<Integer>> result, int[] nums, List<Integer> temp, int index){
96+
result.add(new LinkedList<Integer>(temp));
97+
for(int i = index; i < nums.length; i++){
98+
temp.add(nums[i]);
99+
dfs(result, nums, temp, i + 1);
100+
temp.remove(temp.size() - 1);
101+
}
102+
}
103+
}
104+
```

leetcode/901. Online Stock Span.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 901. Online Stock Span
2+
3+
### Question
4+
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.
5+
6+
The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.
7+
8+
For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].
9+
10+
```
11+
Example 1:
12+
13+
Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
14+
Output: [null,1,1,1,2,1,4,6]
15+
Explanation:
16+
First, S = StockSpanner() is initialized. Then:
17+
S.next(100) is called and returns 1,
18+
S.next(80) is called and returns 1,
19+
S.next(60) is called and returns 1,
20+
S.next(70) is called and returns 2,
21+
S.next(60) is called and returns 1,
22+
S.next(75) is called and returns 4,
23+
S.next(85) is called and returns 6.
24+
25+
Note that (for example) S.next(75) returned 4, because the last 4 prices
26+
(including today's price of 75) were less than or equal to today's price.
27+
```
28+
29+
Note:
30+
* Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.
31+
* There will be at most 10000 calls to StockSpanner.next per test case.
32+
* There will be at most 150000 calls to StockSpanner.next across all test cases.
33+
* The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages.
34+
35+
### Solution:
36+
* Method 1: Monotic stack:
37+
* Keep the stack mono decrease
38+
* Step 1 100 [100, 1] stack is empty and nothing left is small than 100, push [100, 1] to stack.
39+
* Step 2 80 [100, 1][80, 1] 100 > 80, we push[80, 1] to the stack.
40+
* Step 3 60 [100, 1][80, 1][60, 1] same as previous step, we push [60, 1] to the stack.
41+
* Step 4 70 [100, 1][80, 1][70, 2] 60 < 70, so we pop it from the stack and add its count to current one
42+
* Step 5 60 [100, 1][80, 1][70, 2][60, 1] still decreasing, push to stack.
43+
* Step 6 75 [100, 1][80, 1][75, 4] 75 > 60 and 75 > 70, pop that two elements and add their counts to current one.
44+
* Step 7 85 [100, 1][85, 6] same as previous step, pop 80 and 75.
45+
```Java
46+
class StockSpanner {
47+
private Stack<int[]> stack;
48+
public StockSpanner() {
49+
this.stack = new Stack<>();
50+
}
51+
52+
public int next(int price) {
53+
int count = 1;
54+
while(!stack.isEmpty() && stack.peek()[0] <= price){
55+
count += stack.pop()[1];
56+
}
57+
stack.push(new int[]{price, count});
58+
return count;
59+
}
60+
}
61+
/**
62+
* Your StockSpanner object will be instantiated and called as such:
63+
* StockSpanner obj = new StockSpanner();
64+
* int param_1 = obj.next(price);
65+
*/
66+
```

0 commit comments

Comments
 (0)