Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTriangles.java
More file actions
Latest commit
executable file
·163 lines (141 loc) · 5.3 KB
/
Copy pathTriangles.java
File metadata and controls
executable file
·163 lines (141 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
M
1530152837
tags: Array, DP, DFS, Memoization, CoordinateDP
给一个list<list<Integer>> triangle, 细节原题. 找minpathsumfromroot.
#### DFS + Memoization
- 其实跟给一个2Dmatrix没有什么区别, 可以做dfs, memoization.
- initializememo: pathSum[i][j] = MAX_VALUE; 计算过的path省略
- Bottom-top: 先dfs到最深的path, 然后逐步网上返回
- `OR原理: min(pathA, pathB) + currNode`
- 浪费一点空间, pathSum[n][n]. space: O(n^2), wheren = triangleheight
- 时间:O(n^2). Visitallnodesonce: 1 + 2 + 3 + .... n = n^2
#### DP
- 跟dfs的原理很像, `OR原理: min(pathA, pathB) + currNode`
- initdp[n-1][j] = nodevalues
- buildfrombottom -> top: dp[i][j] = Math.min(dp[i + 1][j], dp[i + 1][j + 1]) + triangle.get(i).get(j);
- 跟传统的coordinatedp有所不同, innerforloop是需要计算j <= i, 原因是triangle的性质.
- 空间: dp[n][n]. space: O(n^2)
- 时间:O(n^2). Visitallnodesonce: 1 + 2 + 3 + .... n = n^2
#### DP + O(n) space
- BasedontheDPsolution: thecalculationalwaysdependon `nextrow` forcolat `j` and `j + 1`
- 既然只dependonnextrow, 可以用rollingarray来处理: reducetoO(n) space.
- Further: 可以降维, 把第一维彻底去掉, 变成dp[n]
- 同样是doubleforloop, 但是只在乎columnchanges: `dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);`
```
/*
Given a triangle, find the minimum path sum from top to bottom.
Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
Note:
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.
Thinking process:
1. Bottom-up
- Start from the bottom row, get all values of this row. Note: in triangle, height = cols at each row. So row X has X numbers.
- Start from (n - 1)th row and run up: calculate min from lower level + current node value.
- Depending what is wanted, here we use a 2D int arraya and return the min sum.
*/
publicclassSolution {
//Bottom - up
publicintminimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return0;
}
intn = triangle.size();
int[][] dp = newint[n][n];
// Init bottom row
for (intj = 0; j < n; j++) {
dp[n - 1][j] = triangle.get(n - 1).get(j);
}
for (inti = n - 2; i >= 0; i--) {
for (intj = 0; j <= i; j++) {
dp[i][j] = Math.min(dp[i + 1][j], dp[i + 1][j + 1]) + triangle.get(i).get(j);
}
}
returndp[0][0];
}
}
// Rolling arary
publicclassSolution {
//Bottom - up
publicintminimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return0;
}
intn = triangle.size();
int[][] dp = newint[2][n];
// Init bottom row
for (intj = 0; j < n; j++) {
dp[(n - 1) % 2][j] = triangle.get(n - 1).get(j);
}
for (inti = n - 2; i >= 0; i--) {
for (intj = 0; j <= i; j++) {
dp[i % 2][j] = Math.min(dp[(i + 1)%2][j], dp[(i + 1)%2][j + 1]) + triangle.get(i).get(j);
}
}
returndp[0][0];
}
}
/**
From above solution, row number does not seem to matter during calculation
*/
publicclassSolution {
//Bottom - up
publicintminimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return0;
}
intn = triangle.size();
int[] dp = newint[n];
for (intj = 0; j < n; j++) {
dp[j] = triangle.get(n - 1).get(j);
}
for (inti = n - 2; i >= 0; i--) {
for (intj = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
}
}
returndp[0];
}
}
/*
2. dfs, memoization
- Go through all nodes and initialize with Integer.MAX_VALUE;
- Search from top: pathSum[i][j] = Math.min(dfs(i+1,j), dfs(i+1, j+1)) + currNode
- In dfs: if a node has been set previously, just return this value because this min value has been pre-calculated.
- If row is >= triangle.size(), return 0.
- This method can actually calculate the min sum from bottom to any point in the triangle.
// 94%
*/
publicclassSolution {
int[][] pathSum;
publicintminimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return0;
}
intn = triangle.size();
pathSum = newint[n][n];
for (inti = 0; i < n; i++) {
for (intj = 0; j <= i; j++) {
pathSum[i][j] = Integer.MAX_VALUE;
}
}
returndfs(0, 0, triangle);
}
publicintdfs(inti, intj, List<List<Integer>> triangle) {
if (i >= triangle.size()) {
// each row i should only have i items by triangle definition
return0;
}
if (pathSum[i][j] != Integer.MAX_VALUE) returnpathSum[i][j]; // memoization
pathSum[i][j] = Math.min(dfs(i + 1, j, triangle), dfs(i + 1, j + 1, triangle)) + triangle.get(i).get(j);
returnpathSum[i][j];
}
}
```