Skip to content

Commit 0b41f7e

Browse files
Trapping Rain Water & Spiral Matrix II
1 parent 455331d commit 0b41f7e

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Your ideas/fixes/algorithms are more than welcome!
203203
|68|[Text Justification](https://leetcode.com/problems/text-justification/)|[Solution](../../blob/master/src/stevesun/algorithms/TextJustification.java)|O(n)|O(1)|Hard|
204204
|67|[Add Binary](https://leetcode.com/problems/add-binary/)|[Solution](../../blob/master/src/stevesun/algorithms/AddBinary.java)|O(n)|O(1)|Easy|
205205
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)|[Solution](../../blob/master/src/stevesun/algorithms/PermutationSequence.java)|?|?|Medium|
206+
|59|[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)|[Solution](../../blob/master/src/stevesun/algorithms/SpiralMatrixII.java)|O(n)|O(n)|Medium|
206207
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../../blob/master/src/stevesun/algorithms/LengthofLastWord.java)|O(n)|O(1)|Easy|
207208
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/src/stevesun/algorithms/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
208209
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)|[Solution](../../blob/master/src/stevesun/algorithms/SpiralMatrix.java)|O(m*n)|O(m*n)|Medium|
@@ -211,6 +212,7 @@ Your ideas/fixes/algorithms are more than welcome!
211212
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)|[Solution](../../blob/master/src/stevesun/algorithms/PermutationsII.java)|O(n*n!)|O(n)|Medium|Backtracking
212213
|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[Solution](../../blob/master/src/stevesun/algorithms/JumpGameII.java)|O(?)|O(?)|Hard|
213214
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution]|||Medium
215+
|42|[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)|[Solution](../../blob/master/src/stevesun/algorithms/TrappingRainWater.java)|O(n)|O(1)|Hard|
214216
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)|[Solution](../../blob/master/src/stevesun/algorithms/FirstMissingPositive.java)|O(n)|O(1)|Hard|
215217
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)|[Solution](../../blob/master/src/stevesun/algorithms/CombinationSum.java)|O(k*n^k)|O(k)|Medium|Backtracking
216218
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../../blob/master/src/stevesun/algorithms/CountandSay.java)|O(n*2^n)|O(2^n)|Easy| Recursion, LinkedList
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package stevesun.algorithms;
2+
3+
/**
4+
* Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
5+
6+
For example,
7+
Given n = 3,
8+
9+
You should return the following matrix:
10+
[
11+
[ 1, 2, 3 ],
12+
[ 8, 9, 4 ],
13+
[ 7, 6, 5 ]
14+
]
15+
*/
16+
public class SpiralMatrixII {
17+
18+
public int[][] generateMatrix(int num) {
19+
int temp = num;
20+
int fourEdges[][] = new int[num][num];
21+
int value = 1;
22+
int i = 0, j = 0;
23+
if(num%2 == 0)//when num is even
24+
{
25+
while(i < num/2 && j < num/2 && temp >= 0)
26+
{
27+
/* Assign the top row */
28+
while(j < temp)
29+
{
30+
fourEdges[i][j] = value;
31+
j++;
32+
value++;
33+
34+
}
35+
36+
/* Assign the right column */
37+
while(i < temp - 1){
38+
i++;
39+
fourEdges[i][j - 1] = value;
40+
value++;
41+
}
42+
j = j - 2;
43+
44+
/* Assign the bottom row */
45+
while(j >= num - temp){
46+
fourEdges[i][j] = value;
47+
j--;
48+
value++;
49+
}
50+
i--;
51+
j++;
52+
53+
/* Assign the left column */
54+
while(i > num - temp){
55+
fourEdges[i][j] = value;
56+
i--;
57+
value++;
58+
}
59+
//}
60+
i++;
61+
j++;
62+
temp--;
63+
}
64+
65+
}
66+
else//when num is odd
67+
{
68+
while(i < num/2 && j < num/2 && temp >= 0)
69+
{
70+
/* Assign the top row */
71+
while(j < temp)
72+
{
73+
fourEdges[i][j] = value;
74+
j++;
75+
value++;
76+
77+
}
78+
79+
/* Assign the right column */
80+
while(i < temp - 1){
81+
i++;
82+
fourEdges[i][j - 1] = value;
83+
value++;
84+
}
85+
j = j - 2;
86+
87+
/* Assign the bottom row */
88+
while(j >= num - temp){
89+
fourEdges[i][j] = value;
90+
j--;
91+
value++;
92+
}
93+
i--;
94+
j++;
95+
96+
/* Assign the left column */
97+
while(i > num - temp){
98+
fourEdges[i][j] = value;
99+
i--;
100+
value++;
101+
}
102+
//}
103+
i++;
104+
j++;
105+
temp--;
106+
}
107+
fourEdges[num/2][num/2] = num*num;
108+
}
109+
110+
for(int m = 0; m < num; m++){
111+
for(int n = 0; n < num; n++){
112+
System.out.print(fourEdges[m][n] + "\t");
113+
if((n+1) % num == 0)
114+
System.out.println();
115+
}
116+
}
117+
return fourEdges;
118+
}
119+
120+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package stevesun.algorithms;
2+
3+
/**
4+
* Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
5+
6+
For example,
7+
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
8+
9+
10+
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
11+
*/
12+
public class TrappingRainWater {
13+
14+
public int trap(int[] height) {
15+
int len = height.length;
16+
if(len == 0){
17+
return 0;
18+
}
19+
int res = 0;
20+
21+
//first use DP to calculate out left and right arrays
22+
int[] left = new int[height.length];
23+
int[] right = new int[height.length];
24+
25+
left[0] = height[0];
26+
for(int i = 1; i < height.length; i++){
27+
left[i] = Math.max(left[i-1], height[i]);
28+
}
29+
30+
right[height.length-1] = height[height.length-1];
31+
for(int i = height.length-2; i >= 0; i--){
32+
right[i] = Math.max(right[i+1], height[i]);
33+
}
34+
35+
for(int i = 1; i < height.length-1; i++){
36+
res += Math.min(left[i], right[i]) - height[i];
37+
}
38+
return res;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)