-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathNo120.triangle.cs
65 lines (58 loc) · 1.91 KB
/
No120.triangle.cs
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
/*
* Difficulty:
* Medium
*
* Desc:
* 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.
*/
public class Solution {
public int MinimumTotal_DFS(IList<IList<int>> triangle) {
if (triangle.Count == 0) return 0;
if (triangle[0].Count == 0) return 0;
int[,] cache = new int[triangle.Count, triangle[^1].Count];
int Dfs(int row, int col) {
if (col >= triangle[row].Count) return int.MaxValue;
if (row == triangle.Count - 1) return triangle[row][col];
if (cache[row, col] != 0) return cache[row, col];
int res = triangle[row][col] + Math.Min(
Dfs(row + 1, col),
Dfs(row + 1, col + 1)
);
cache[row, col] = res;
return res;
}
return Dfs(0, 0);
}
public int MinimumTotal_DP(IList<IList<int>> triangle) {
if (triangle.Count == 0) return 0;
if (triangle[0].Count == 0) return 0;
int[] dp = new int[0];
for (int i = 0; i < triangle.Count; i += 1) {
int[] cache = new int[triangle[i].Count];
for (int j = 0; j < triangle[i].Count; j += 1) {
int min = triangle[i][j];
if (i > 0) {
min += Math.Min(
j >= dp.Length ? int.MaxValue : dp[j],
j > 0 ? dp[j - 1] : int.MaxValue
);
}
cache[j] = min;
}
dp = cache;
}
return dp.Min();
}
}