Skip to content

Commit 0085d5b

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 9dccda8 commit 0085d5b

6 files changed

+343
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167

168168
[84. Largest Rectangle in Histogram](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/84.%20Largest%20Rectangle%20in%20Histogram.md)
169169

170+
[85. Maximal Rectangle](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/85.%20Maximal%20Rectangle.md)
171+
170172
[86. Partition List](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/86.%20Partition%20List.md)
171173

172174
[88. Merge Sorted Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/88.%20Merge%20Sorted%20Array.md)
@@ -185,6 +187,8 @@
185187

186188
[96. Unique Binary Search Trees](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/96.%20Unique%20Binary%20Search%20Trees.md)
187189

190+
[97. Interleaving String](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/97.%20Interleaving%20String.md)
191+
188192
[98. Validate Binary Search Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/98.%20Validate%20Binary%20Search%20Tree.md)
189193

190194
[100. Same Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/100.%20Same%20Tree.md)
@@ -483,6 +487,8 @@
483487

484488
[309. Best Time to Buy and Sell Stock with Cooldown](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/309.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown.md)
485489

490+
[310. Minimum Height Trees](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/310.%20Minimum%20Height%20Trees.md)
491+
486492

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

leetcode/221. Maximal Square.md

+26
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,30 @@ class Solution {
8686
return max * max;
8787
}
8888
}
89+
```
90+
91+
### Third time
92+
1. Still use the same method, without hint.
93+
2. Optimized the solution.
94+
```Java
95+
class Solution {
96+
public int maximalSquare(char[][] matrix) {
97+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
98+
int max = 0;
99+
int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
100+
for(int i = 1; i <= matrix.length; i++){
101+
for(int j = 1; j <= matrix[0].length; j++){
102+
if(matrix[i - 1][j - 1] == '0') dp[i][j] = 0;
103+
else{
104+
if(dp[i - 1][j] == dp[i - 1][j - 1] && dp[i][j - 1] == dp[i - 1][j - 1])
105+
dp[i][j] = dp[i - 1][j - 1] + 1;
106+
else
107+
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
108+
max = Math.max(max, dp[i][j]);
109+
}
110+
}
111+
}
112+
return max * max;
113+
}
114+
}
89115
```

leetcode/310. Minimum Height Trees.md

+94
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,98 @@ class Solution {
125125
return leaves;
126126
}
127127
}
128+
```
129+
130+
### Second time
131+
1. BFS, AC, almost TLE
132+
```Java
133+
class Solution {
134+
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
135+
List<Integer>[] bag = new List[n];
136+
for(int i = 0; i < n; i++){
137+
bag[i] = new LinkedList<Integer>();
138+
}
139+
for(int[] pair : edges){
140+
bag[pair[0]].add(pair[1]);
141+
bag[pair[1]].add(pair[0]);
142+
}
143+
int min = Integer.MAX_VALUE;
144+
List<Integer> result = new LinkedList<>();
145+
for(int i = 0; i < n; i++){
146+
boolean[] visited = new boolean[n];
147+
LinkedList<Integer> queue = new LinkedList<>();
148+
queue.add(i);
149+
int count = 0;
150+
while(!queue.isEmpty()){
151+
int size = queue.size();
152+
for(int j = 0; j < size; j++){
153+
Integer v = queue.poll();
154+
visited[v] = true;
155+
for(Integer node : bag[v]){
156+
if(!visited[node]){
157+
queue.add(node);
158+
}
159+
}
160+
}
161+
count++;
162+
}
163+
if(count == min) result.add(i);
164+
if(count < min){
165+
result.clear();
166+
result.add(i);
167+
min = count;
168+
}
169+
}
170+
return result;
171+
}
172+
}
173+
```
174+
175+
2. Calculate the indegree of each node, if indegree is 1, add to queue.
176+
* once the remained node number is small or equal to 2, break the queue.
177+
```Java
178+
class Solution {
179+
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
180+
List<Integer> result = new LinkedList<>();
181+
if(n <= 2){
182+
for(int i = 0; i < n; i++) result.add(i);
183+
return result;
184+
}
185+
int[] count = new int[n];
186+
boolean[] visited = new boolean[n];
187+
List<Integer>[] bag = new List[n];
188+
for(int i = 0; i < n; i++) bag[i] = new LinkedList<>();
189+
for(int[] pair : edges){
190+
count[pair[0]]++;
191+
bag[pair[0]].add(pair[1]);
192+
count[pair[1]]++;
193+
bag[pair[1]].add(pair[0]);
194+
}
195+
LinkedList<Integer> queue = new LinkedList<>();
196+
int v = n;
197+
for(int i = 0; i < n; i++){
198+
if(count[i] == 1){
199+
queue.add(i);
200+
count[i]--;
201+
}
202+
}
203+
while(!queue.isEmpty() && v > 2){
204+
int size = queue.size();
205+
v -= size;
206+
for(int i = 0; i < size; i++){
207+
int val = queue.poll();
208+
visited[val] = true;
209+
List<Integer> list = bag[val];
210+
for(int node: list){
211+
count[node]--;
212+
if(count[node] == 1) queue.add(node);
213+
}
214+
}
215+
}
216+
for(int i = 0; i < n; i++){
217+
if(!visited[i]) result.add(i);
218+
}
219+
return result;
220+
}
221+
}
128222
```

leetcode/84. Largest Rectangle in Histogram.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,45 @@ class Solution {
5555
return result;
5656
}
5757
}
58-
```
58+
```
59+
60+
### Second time
61+
1. Monostack
62+
```Java
63+
/**
64+
[0,1,2,3,4,5]
65+
[2,1,5,6,2,3]
66+
action stack(save the index) area max
67+
1. 2 add to stack 2 0 0
68+
2. 1 pop 2 1 2 2
69+
3. 5 add to stack 1, 5 2 2
70+
4. 6 add to stack 1, 5, 6 2 2
71+
5. 2 pop 6 from stack 1, 5 6 6
72+
pop 5 from stack 1 10 10
73+
add 2 to stack 1, 2 0 0
74+
6. 3 add 3 to stack 1, 2, 3 0 0
75+
7. 0 pop 3 from stack 1, 2 3 10
76+
pop 2 from stack 1 4 10
77+
pop 1 from stack empty 6 10
78+
*/
79+
class Solution {
80+
public int largestRectangleArea(int[] heights) {
81+
if(heights == null || heights.length == 0) return 0;
82+
int res = 0;
83+
Stack<Integer> stack = new Stack<>();
84+
for(int i = 0; i <= heights.length; i++){
85+
int h = i == heights.length ? 0: heights[i]; // add a 0 to the last so we have a chance to deal with the last mono inscrease part.
86+
while(!stack.isEmpty() && h < heights[stack.peek()]){ // pop the value larger than current one to keep the monostack.
87+
int height = heights[stack.pop()];
88+
int index = stack.isEmpty() ? -1: stack.peek(); // if empty, means current value is the minimum
89+
res = Math.max(res, height * (i - index - 1)); // get the local area
90+
}
91+
stack.push(i); // always add current index to the stack.
92+
}
93+
return res;
94+
}
95+
}
96+
```
97+
98+
### Reference
99+
1. [Leetcode : 84. Largest Rectangle in Histogram 讲解](https://www.youtube.com/watch?v=TH9UaZ6JGcA)

leetcode/85. Maximal Rectangle.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
## 85. Maximal Rectangle
2+
3+
### Question
4+
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
5+
6+
```
7+
Example:
8+
9+
Input:
10+
[
11+
["1","0","1","0","0"],
12+
["1","0","1","1","1"],
13+
["1","1","1","1","1"],
14+
["1","0","0","1","0"]
15+
]
16+
Output: 6
17+
```
18+
19+
### Thinking:
20+
* Method 1: Use the method of Monostack as question 84: [84. Largest Rectangle in Histogram](https://seanforfun.github.io/leetcode/2018/11/07/84.LargestRectangleinHistogram.html)
21+
* we use a height array to save the historgram.
22+
* if current value is 1, we increase the value saved in that index.
23+
* if current value is 0, we set the value to 0.
24+
```Java
25+
class Solution {
26+
public int maximalRectangle(char[][] matrix) {
27+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
28+
int res = 0;
29+
int[] height = new int[matrix[0].length + 1];
30+
for(int row = 0; row < matrix.length; row++){
31+
for(int i = 0; i < matrix[0].length; i++){
32+
if(matrix[row][i] == '1') height[i]++;
33+
else height[i] = 0;
34+
}
35+
Stack<Integer> stack = new Stack<>();
36+
for(int j = 0; j <= matrix[0].length; j++){
37+
while(!stack.isEmpty() && height[j] < height[stack.peek()]){
38+
int h = height[stack.pop()];
39+
int index = stack.isEmpty() ? -1: stack.peek();
40+
res = Math.max(res, h * (j - index - 1));
41+
}
42+
stack.push(j);
43+
}
44+
}
45+
return res;
46+
}
47+
}
48+
```
49+
50+
* Method 2: dp
51+
* we define 3 arrays for each row:
52+
* left(save the first index of 1 in current row, still need to compare with previous value), initial value is 0.
53+
* right(save the last index of 1 in current row, need to compare with previous value), initialized with len.
54+
* height(record the height as historgram)
55+
* area = height * (right - left)
56+
```Java
57+
/**
58+
Example:
59+
0 1 2 3 4
60+
row0 ["1","0","1","0","0"],
61+
row1 ["1","0","1","1","1"],
62+
row2 ["1","1","1","1","1"],
63+
row3 ["1","0","0","1","0"]
64+
65+
height[]: height array saves the historgram for current row.
66+
row0 [1,0,1,0,0],
67+
row1 [2,0,2,1,1],
68+
row2 [3,1,3,2,2],
69+
row3 [4,0,0,3,0]
70+
71+
left[]: left array saves the first index in current row and we need to compare it with the previous row left array.
72+
row0 [0,0,2,0,0],
73+
row1 [0,0,2,2,2],
74+
row2 [0,0,2,2,2],
75+
row3 [0,0,0,3,0]
76+
77+
right[]: right array saves the last index in current row(need to compare it with the previous row right array.)
78+
row0 [1,5,3,5,5],
79+
row1 [1,5,3,5,5],
80+
row2 [1,5,3,5,5],
81+
row3 [1,5,5,4,5]
82+
*/
83+
class Solution {
84+
public int maximalRectangle(char[][] matrix) {
85+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
86+
int res = 0, len = matrix[0].length;
87+
int[] left = new int[len];
88+
int[] right = new int[len];
89+
int[] height = new int[len];
90+
for(int i = 0; i < len; i++) right[i] = len;
91+
for(int row = 0; row < matrix.length; row++){
92+
int cur_left = 0, cur_right = len;
93+
for(int i = 0; i < len; i++){
94+
if(matrix[row][i] == '0') height[i] = 0;
95+
else height[i]++;
96+
}
97+
for(int i = 0; i < len; i++){
98+
if(matrix[row][i] == '1'){
99+
left[i] = Math.max(left[i], cur_left);
100+
}else{
101+
cur_left = i + 1;
102+
left[i] = 0;
103+
}
104+
}
105+
for(int i = len - 1; i >= 0; i--){
106+
if(matrix[row][i] == '1'){
107+
right[i] = Math.min(right[i], cur_right);
108+
}else{
109+
cur_right = i;
110+
right[i] = len;
111+
}
112+
}
113+
for(int i = 0; i < len; i++){
114+
res = Math.max(res, height[i] * (right[i] - left[i]));
115+
}
116+
}
117+
return res;
118+
}
119+
}
120+
```
121+
122+
123+
### Reference
124+
1. [Leetcode : 85. Maximal Rectangle 讲解](https://www.youtube.com/watch?v=5CEBM_174e0)

leetcode/97. Interleaving String.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## 97. Interleaving String
2+
3+
### Question
4+
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
5+
6+
```
7+
Example 1:
8+
9+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
10+
Output: true
11+
12+
Example 2:
13+
14+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
15+
Output: false
16+
```
17+
18+
### Thinking:
19+
* Method 1:dp, this is the first time that I solved a dp hard question without any hint.
20+
* how to create the dp array: dp[i][j], where i mean s1 provides the ith character, j means s2 provides the jth character, currently, length of created string is i + j - 1;
21+
* initialization:
22+
* dp[0][0] = true.
23+
* for i == 0, means the string is fully constructed by s2, dp[0][j] = dp[0][j - 1] && (s2.charAt(j - 1) == s3.charAt(j - 1)), same thing in j = 0. Or we can just use startsWith to check.
24+
* for dp[i][j]: either one works is fine.
25+
* dp[i - 1][j] && (s3.charAt(i + j - 1) == s1.charAt(i - 1))
26+
* dp[i][j - 1] && (s3.charAt(i + j - 1) == s2.charAt(j - 1))
27+
```Java
28+
class Solution {
29+
public boolean isInterleave(String s1, String s2, String s3) {
30+
if(s1 == null || s2 == null || s3 == null) return false;
31+
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
32+
if(len1 + len2 != len3) return false;
33+
boolean[][] dp = new boolean[len1 + 1][len2 + 1];
34+
char[] s3Char = s3.toCharArray();
35+
dp[0][0] = true;
36+
for(int i = 1; i <= len1; i++){
37+
dp[i][0] = dp[i - 1][0] && (s3Char[i - 1] == s1.charAt(i - 1));
38+
}
39+
for(int i = 1; i <= len2; i++){
40+
dp[0][i] = dp[0][i - 1] && (s3Char[i - 1] == s2.charAt(i - 1));
41+
}
42+
for(int i = 1; i <= len1; i++){
43+
for(int j = 1; j <= len2; j++){
44+
dp[i][j] = (dp[i][j - 1] && (s3Char[i + j - 1] == s2.charAt(j - 1)))
45+
|| (dp[i - 1][j] && (s3Char[i + j - 1] == s1.charAt(i - 1)));
46+
}
47+
}
48+
return dp[len1][len2];
49+
}
50+
}
51+
```

0 commit comments

Comments
 (0)