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.
思路:
上面的三角形可以看成如下样子:
2
3 4
6 5 7
4 1 8 3
按题中的要求,只能往下访问相邻的数字,和5向下相邻的数字是1和8,和7向下相邻的数字是8和3。
而5和1所处的索引相同,8的索引为5的索引加1。
7和8所处的索引相同,3的索引为7的索引加1。
那么我们可以从下往上得出最小和的三角形:
11 // 11=min(2+9, 2+10)
9 10 // 9=min(3+7, 3+6), 10=min(4+6, 4+10)
7 6 10 // 7=min(6+4, 6+1), 6=min(5+1, 5+8), 10=min(7+8, 7+3)
4 1 8 3
那么动态规划方程就是:
F[i] = V[i] + min(F[i]+F[i+1]), 0 <= i < size of every line
F[i]代表从下往上到索引 i 处的最小和,V[i]代表每行第i个数字的值。
那么最后的最小和结果就位于F[0]处。
代码:
public int minimumTotal(List<List<Integer>> triangle) { int h = triangle.size(); //三角形的高度 int w = triangle.get(h-1).size(); //三角形的底,即最后一行的长度 int[] f = new int[w]; // vertical sum,bottom up for(int i=0; i<w; i++) { f[i] = triangle.get(h-1).get(i); } for(int i=h-2; i>=0; i--) { //从倒数第二行开始,往上遍历 List<Integer> line = triangle.get(i); for(int j=0; j<=i; j++) { //第i行的个数为i+1,所以j的最大值为i, 也就是i==triangle.get(i).size()-1 f[j] = line.get(j) + Math.min(f[j], f[j+1]); } } return f[0]; }
又重新写了一遍这题,发现代码可以更简单:
public int minimumTotal(List<List<Integer>> triangle) { int n = triangle.size(); int[] f = new int[n+1]; for(int i=n-1; i>=0; i--) { //注意,j一定要从0到i,不然会把后面的元素加进来 for(int j=0; j<=i; j++) { f[j] = triangle.get(i).get(j) + Math.min(f[j], f[j+1]); } } return f[0]; }
相关推荐
LeetCode-Triangle
120 Triangle 2020-01-15 213 House Robberll -变种 198 337 2020-01-16 139 单词拆分 2020-01-20 104 树 -变种 111 2020-01-21 129 Sum root to leaf numbers 2020-01-22 226 翻转二叉树 2020-01-23 95 不同的二叉...
leetcode双人赛 leetcode-solution 闲暇之余,刷一下题,弥补数据结构和算法的短板 概述 之前写过一个 leetcode 的一点题解,不过后来就断了,现在重新开个项目:party_popper::party...pascals-triangle 杨辉三角 II pa
120. Triangle LintCode 382. Triangle Count/LeetCode 611. Valid Triangle Number LeetCode 18. 4Sum (LeetCode 86. Partition List) LintCode 373. Partition Array by Odd and Even Mock Interview 题目 ...
leetcode 分类 LeetCode Progress ...Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
Triangle Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space (size that is greater or equal to m + n)to hold additional elements ...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of Binary Tree 栈 evaluate-reverse-polish-notation 链表 sort-list 排序 insertion-sort-list 树 binary-tree...
Triangle (杨辉三角) 124 二叉树最大路径和 136 x ^ x = 0 169 Majority Vote Algorithm (最大投票数算法) 240 检索二阶矩阵 189 数组操作的时间复杂度比较 206 反转单向链表 226 反转二叉树 459 重复子字符串模式...
leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection Question) 准备方式 分析tag的热门标签,熟悉各个标签解题的思路(解决该标签全部的easy和medium为主),再...
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
leetcode========leetcode 题解,更新中.提供Java和 Python 版本的solution为了让更多的同学了解并应用Leetcode,...ii/简单数学:http://oj.leetcode.com/problems/pascals-triangle/http://oj.leetcode.com/proble
leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by JavaScript and TypeScript. easy 66.加一 (Plus One) 67.二进制求和 (Add Binary) 69.x 的平方根 (Sqrt(x)) 70.爬楼梯 ...
2sum leetcode leetcode 文件和描述 removeelement.cpp,删除特定元素 ...triangle.cpp,动态方法从三角形中找到从上到下的最小路径总和 maxsubarray.cpp,动态方法从数组中找到连续子数组的最大和
圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -> 使用哈希表避免遍历列表448.查找数组中消失的所有数字-> 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....
leetcode 刷题笔记 记录一些刷题细节,很惭愧只做了一点微小的工作 4.13 162题. Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. ...
主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
颜色分类leetcode 形状 :blue_circle: :large_orange_diamond: :red_triangle_pointed_up: :red_circle: 一个示例数据集生成器,用于在使用真实世界的数据集进行测试之前,对计算机视觉模型进行分类、检测和分割试验...
leetcode添加元素使和等于 leetCode 递归 95 能够想到用递归左右产生子树的方法...120 和矩阵的最小路径(64 )相同,用路径相加求最小值的方式 int[] result = new int[triangle.size() + 1]; for (int i = triangle.
谷歌师兄的leetcode刷题笔记免费 这是一个自定义 Web 应用程序,用于收集有关 Penn State Chapter of Triangle 奖学金候选人的信息。 它旨在通过使候选人更容易提供结构化信息以及审查者访问该信息来促进收集过程的...