您的位置:首页 > 其它

120. Triangle

2016-02-18 04:15 323 查看
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.

Solution 1

Straightforward way Top to bottom 14ms 6.70%

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(triangle.get(0).get(0));
for (int i = 1; i < triangle.size(); i++) {
ArrayList<Integer> temp = new ArrayList<Integer>();
int length = i + 1;
for (int j = 0; j < length; j++) {
if (j == 0) {
temp.add(triangle.get(i).get(j) + list.get(0));
}else if (j == length - 1) {
temp.add(triangle.get(i).get(j) + list.get(j - 1));
}else {
temp.add(Math.min(triangle.get(i).get(j) + list.get(j - 1), triangle.get(i).get(j) + list.get(j)));
}
}
list = temp;
}
Collections.sort(list);
return list.get(0);
}
}Solution 2 Bottom to top
12ms 13.62%

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
for (int i = triangle.size() - 2; i >= 0; i--)
for (int j = 0; j <= i; j++)
triangle.get(i).set(j,
triangle.get(i).get(j) + Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1)));
return triangle.get(0).get(0);
}
}

Solution 3 Bottom to top 



4ms 72.96%

public class Solution {
private static int n;
private static int[][] minSum;
private static List<List<Integer>> triangle;

public int search(int x, int y){
if(x >= n){
return 0;
}
if(minSum[x][y] != Integer.MAX_VALUE){
return minSum[x][y];
}
minSum[x][y] = Math.min(search(x + 1, y), search(x + 1, y + 1)) + triangle.get(x).get(y);
return minSum[x][y];
}

public int minimumTotal(List<List<Integer>> triangle) {
this.triangle = triangle;
n = triangle.size();
minSum = new int

;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
minSum[i][j] = Integer.MAX_VALUE;
}
}

return search(0, 0);
}
}Solution 4 Much like the first solution, but the first is iterative this is recursive
 99.70% 1mspublic static int minimumTotal4(List<List<Integer>> triangle) {
if (triangle.size() == 0)
return 0;
if (triangle.size() == 1)
return triangle.get(0).get(0);

int[] dp = new int[triangle.size()];
dp[0] = triangle.get(0).get(0);
return minimumTotal(triangle, dp, 1);
}

public static int minimumTotal(List<List<Integer>> triangle, int[] dp, int lvlidx) {
List<Integer> list = triangle.get(lvlidx);
int pre = dp[0], temp;
dp[0] += list.get(0);
for (int i = 1; i < lvlidx; i++) {
temp = dp[i];
dp[i] = list.get(i) + Math.min(pre, dp[i]);
pre = temp;
}
dp[lvlidx] = pre + list.get(lvlidx);

if (lvlidx + 1 == triangle.size()) {
int res = dp[0];
for (int i = 1; i <= lvlidx; i++)
res = Math.min(res, dp[i]);
return res;
}

return minimumTotal(triangle, dp, lvlidx + 1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: