120:三角形最小路径和

This commit is contained in:
huangge1199 2021-07-19 14:05:44 +08:00
parent 6d7a1077b4
commit f149152b3a
2 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,101 @@
//给定一个三角形 triangle 找出自顶向下的最小路径和
//
// 每一步只能移动到下一行中相邻的结点上相邻的结点 在这里指的是 下标 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点也就是说如果
//正位于当前行的下标 i 那么下一步可以移动到下一行的下标 i i + 1
//
//
//
// 示例 1
//
//
//输入triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
//输出11
//解释如下面简图所示
// 2
// 3 4
// 6 5 7
//4 1 8 3
//自顶向下的最小路径和为 112 + 3 + 5 + 1 = 11
//
//
// 示例 2
//
//
//输入triangle = [[-10]]
//输出-10
//
//
//
//
// 提示
//
//
// 1 <= triangle.length <= 200
// triangle[0].length == 1
// triangle[i].length == triangle[i - 1].length + 1
// -104 <= triangle[i][j] <= 104
//
//
//
//
// 进阶
//
//
// 你可以只使用 O(n) 的额外空间n 为三角形的总行数来解决这个问题吗
//
// Related Topics 数组 动态规划
// 👍 793 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.List;
//120:三角形最小路径和
public class Triangle {
public static void main(String[] args) {
//测试代码
Solution solution = new Triangle().new Solution();
System.out.println(solution.minimumTotal(Arrays.asList(
Arrays.asList(2),
Arrays.asList(3, 4),
Arrays.asList(6, 5, 7),
Arrays.asList(4, 1, 8, 3)
)));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
// private int min = Integer.MAX_VALUE;
//
// public int minimumTotal(List<List<Integer>> triangle) {
// dfs(triangle, 0, 0, 0);
// return min;
// }
//
// private void dfs(List<List<Integer>> triangle, int index, int count, int sum) {
// sum += triangle.get(count).get(index);
// count++;
// if (triangle.size() == count) {
// min = Math.min(min, sum);
// return;
// }
// List<Integer> list = triangle.get(count);
// dfs(triangle, index, count, sum);
// dfs(triangle, index + 1, count, sum);
// }
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
int[] dp = new int[n + 1];
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
}
}
return dp[0];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,45 @@
<p>给定一个三角形 <code>triangle</code> ,找出自顶向下的最小路径和。</p>
<p>每一步只能移动到下一行中相邻的结点上。<strong>相邻的结点 </strong>在这里指的是 <strong>下标</strong><strong>上一层结点下标</strong> 相同或者等于 <strong>上一层结点下标 + 1</strong> 的两个结点。也就是说,如果正位于当前行的下标 <code>i</code> ,那么下一步可以移动到下一行的下标 <code>i</code><code>i + 1</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>输出:</strong>11
<strong>解释:</strong>如下面简图所示:
<strong>2</strong>
<strong>3</strong> 4
6 <strong>5</strong> 7
4 <strong>1</strong> 8 3
自顶向下的最小路径和为 112 + 3 + 5 + 1 = 11
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>triangle = [[-10]]
<strong>输出:</strong>-10
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以只使用 <code>O(n)</code> 的额外空间(<code>n</code> 为三角形的总行数)来解决这个问题吗?</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 793</li><li>👎 0</li></div>