您的位置:首页 > 其它

leetcode120. Triangle

2017-04-16 13:36 288 查看

120. Triangle

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).


解法一

自底向上,动态规划

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return -1;
}
if (triangle.get(0) == null || triangle.get(0).size() == 0) {
return -1;
}

// state: f.get(x).get(y) = minimum path value from x,y to bottom
int len = triangle.size();
// initail a new list
List<List<Integer>> minSum = new ArrayList<>();
List<Integer> temp = null;
for (int i = 0; i < len; i++) {
temp = new ArrayList<>();
for (int j = 0; j < triangle.get(i).size(); j++) {
temp.add(j, 0);
}
minSum.add(i, temp);
}
// initial last row
for (int j = 0; j < triangle.get(len - 1).size(); j++) {
minSum.get(len - 1).set(j, triangle.get(len - 1).get(j));
}
// bottom up
for (int i = len - 2; i >= 0; i--) {
for (int j = 0; j < minSum.get(i).size(); j++) {
minSum.get(i).set(j, Math.min(minSum.get(i + 1).get(j), minSum.get(i + 1).get(j + 1))
+ triangle.get(i).get(j));
}
}

return minSum.get(0).get(0);
}
}




解法二

直接在原list中进行变化。

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return -1;
}
if (triangle.get(0) == null || triangle.get(0).size() == 0) {
return -1;
}

// state: f.get(x).get(y) = minimum path value from x,y to bottom
int len = triangle.size();
// bottom up
for (int i = len - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
triangle.get(i).set(j, Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1))
+ triangle.get(i).get(j));
}
}

return triangle.get(0).get(0);
}
}




解法三

up bottom,新初始化一个列表。

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return -1;
}
if (triangle.get(0) == null || triangle.get(0).size() == 0) {
return -1;
}

// state: f.get(x).get(y) = minimum path value from x,y to bottom
// initial new list
int len = triangle.size();
List<List<Integer>> minSum = new ArrayList<>();
List<Integer> temp = null;
for (int i = 0; i < len; i++) {
temp = new ArrayList<>();
for (int j = 0; j < triangle.get(i).size(); j++) {
temp.add(j, 0);
}
minSum.add(i, temp);
}

// initial
minSum.get(0).set(0, triangle.get(0).get(0));
for (int i = 1; i < len; i++) {
minSum.get(i).set(0, minSum.get(i - 1).get(0) + triangle.get(i).get(0));
minSum.get(i).set(i, minSum.get(i - 1).get(i - 1) + triangle.get(i).get(i));
}
// up bottom
for (int i = 1; i < len; i++) {
for (int j = 1; j < i; j++) {
minSum.get(i).set(j, Math.min(minSum.get(i - 1).get(j - 1), minSum.get(i - 1).get(j))
+ triangle.get(i).get(j));
}
}

int best = minSum.get(len - 1).get(0);
for (int i = 0; i < len; i++) {
if (minSum.get(len - 1).get(i) < best)
best = minSum.get(len - 1).get(i);

}
return best;
}
}




解法四

up bottom基于原列表list

public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return -1;
}
if (triangle.get(0) == null || triangle.get(0).size() == 0) {
return -1;
}

// state: f.get(x).get(y) = minimum path value from 0,0 to x,y
int len = triangle.size();
// up bottom
for (int i = 1; i < len; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0) {
triangle.get(i).set(j, triangle.get(i - 1).get(j)
+ triangle.get(i).get(j));
} else if (j == i) {
triangle.get(i).set(j, triangle.get(i - 1).get(j - 1)
+ triangle.get(i).get(j));
} else {
triangle.get(i).set(j, Math.min(triangle.get(i - 1).get(j - 1), triangle.get(i - 1).get(j))
+ triangle.get(i).get(j));
}
}
}

int best = triangle.get(len - 1).get(0);
for (int i = 0; i < len; i++) {
if (triangle.get(len - 1).get(i) < best)
best = triangle.get(len - 1).get(i);

}
return best;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: