Skip to content

Commit 174ec34

Browse files
committed
The most verbose code ever
1 parent 713b4d8 commit 174ec34

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/MaximalRectangle/Solution.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ public int maximalRectangle_error(char[][] matrix) {
8484
for(int k=i; k>i-vertical[i+1][j+1]; k--)
8585
length = Math.min(length, horizontal[k+1][j+1]);
8686
maxa = Math.max(maxa, Math.max(horizontal[i+1][j+1]*height, vertical[i+1][j+1]*length)); // logic flaw
87-
if(maxa==12) {
88-
System.out.println(maxa);
89-
}
9087
}
9188

9289
return maxa;

src/Triangle/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Triangle;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
/**
7+
* User: Danyang
8+
* Date: 1/26/2015
9+
* Time: 23:52
10+
*
11+
* Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row
12+
* below.
13+
14+
For example, given the following triangle
15+
[
16+
[2],
17+
[3,4],
18+
[6,5,7],
19+
[4,1,8,3]
20+
]
21+
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
22+
23+
Note:
24+
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
25+
*/
26+
public class Solution {
27+
/**
28+
* dp
29+
* let f[i][j] represent the minimal path sum from bottom to i, j
30+
* f[i][j] = min(f[i+1][j], f[i+1][j+1])+t[i][j]
31+
* @param triangle
32+
* @return
33+
*/
34+
public int minimumTotal(List<List<Integer>> triangle) {
35+
List<List<Integer>> f = triangle.stream()
36+
.map(e -> e.stream().map(e2->0).collect(Collectors.toList()))
37+
.collect(Collectors.toList());
38+
for(int i=0; i<triangle.get(triangle.size()-1).size(); i++) {
39+
f.get(f.size()-1).set(i, triangle.get(triangle.size()-1).get(i));
40+
}
41+
for(int i=triangle.size()-2; i>-1; i--) {
42+
for(int j=0; j<triangle.get(i).size(); j++) {
43+
int min = Math.min(f.get(i+1).get(j), f.get(i+1).get(j+1))+triangle.get(i).get(j);
44+
f.get(i).set(j, min);
45+
}
46+
}
47+
return f.get(0).get(0);
48+
}
49+
}

0 commit comments

Comments
 (0)