LeetCode(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
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
分析
本题类似于之前的一个障碍物的题目,用到动态规划的思想;
分析第i层的第k个顶点的最小路径长度表示为
f(i,k),则f(i,k)=minf(i−1,k),f(i−1,k−1)+d(i,k); (注意每行的首尾边界需要特殊处理)
其中d(i,k)表示原来三角形数组里的第i行第k列的元素。
则可以求得从第一行到最终到第rows−1行第k个元素的最小路径长度,最后再比较第rows−1行中所有元素的路径长度大小,求得最小值。
题目要求:空间复杂度不要超过n。
AC代码
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if (triangle.empty())
return 0;
int rows = triangle.size();
//动态规划,由于空间复杂度要求,现利用原始二维数组triangle改为存储当前(i,j)位置的最小和
for (int i = 1; i < rows; ++i)
{
int cols = triangle[i].size();
for (int j = 0; j < cols; ++j)
{
//本行的第一个元素
if (0 == j)
{
triangle[i][j] = triangle[i][j] + triangle[i - 1][j];
}
//本行的最后一个元素
else if (j == cols - 1)
{
triangle[i][j] += triangle[i - 1][j - 1];
}
else{
triangle[i][j] = min(triangle[i][j] + triangle[i][j - 1], triangle[i][j] + triangle[i - 1][j - 1]);
}//else
}//for
}//for
//最小路径和为最后一行的最小值
int minSum = triangle[rows - 1][0];
for (int j = 0; j < triangle[rows - 1].size(); ++j)
{
if (minSum > triangle[rows - 1][j])
minSum = triangle[rows - 1][j];
}//for
return minSum;
}
};
LeetCode(120) Triangle的更多相关文章
- LeetCode(120):三角形最小路径和
Medium! 题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- NETCORE MVC模块化
NETCORE MVC模块化 ASP.NETCORE MVC模块化编程 前言 记得上一篇博客中跟大家分享的是基于ASP.NETMVC5,实际也就是基于NETFRAMEWORK平台实现的这么一个轻量级插 ...
- 这个匿名对象没有实现IComparable接口
https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...
- 《java学习三》并发编程 -------线程池原理剖析
阻塞队列与非阻塞队 阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞.试图从空的阻塞队列中获取元素的线程将会被阻塞,直到 ...
- Oracle批量SQL之 BULK COLLECT 子句
BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING ...
- Code First 2
在codefirst一中也说了Mapping是实体与数据库的纽带,model通过Mapping映射到数据库,我们可以从数据库的角度来分析?首先是映射到数据库,这个是必须的.数据库里面一般包括表.列.约 ...
- Random类、ThreadLocalRandom类
Random和ThreadLocalRandom类均用于生成伪随机数. Random的构造函数: Random() 默认以系统当前时间为种子,相当于Random(System.currentT ...
- Java面向对象(接口、多态)
面向对象 今日内容介绍 u 接口 u 多态 第1章 接口 1.1 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的 ...
- css3实现钟表效果
利用css3 transform属性刻画钟表的的刻度以及指针的角度,代码如下: <head> <meta charset="UTF-8"> <titl ...
- layout_weight属性
layout_weight 某个控件text多行,第一行对齐,baselineAligned = "false" 可使控件对齐 weight 计算规则 剩余尺寸=父布局尺寸-子布局 ...
- django之session配置
session应用示例 from django.shortcuts import render from django.shortcuts import HttpResponse from djang ...