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
The minimum path sum from top to bottom is
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);
}
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);
}
相关文章推荐
- [LeetCode 024] Swap Nodes in Pairs
- protobuf NewCallback 在 proto version 3.0中的位置有所变化
- Python将数组(矩阵)存成csv文件,将csv文件读取为数组(矩阵)
- Java中带条件的查询
- Java中字符串相等与大小比较
- Android之Service
- JavaScript 笔记 9 - Date对象
- 搭建分布式架构5--ZooKeeper 集群的安装 3ff0
- c语言:用二分法求方程在(-10,10)之间的根:2x^3-4x^2+3x-6=0.
- 63. Unique Paths II
- [Help] Proximal mapping
- c语言:用牛顿迭代法求方程在1.5附近的根:2x^3-4x^2+3x-6=0.
- mongoose 文档(九) Plugins
- 1088. Rational Arithmetic (20)
- Zone.js
- 1087. All Roads Lead to Rome (30)
- DBManager类(java jdbc数据库连接及操作)
- c语言:用迭代法求x=a^0.5,迭代公式为x1 = (x0 + a / x0) / 2;前后两次求出的x的差的绝对值小于10^-5
- m*n矩阵转换为n*m矩阵
- 游戏还有游戏还是游戏