博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 120: Triangle
阅读量:4565 次
发布时间:2019-06-08

本文共 1001 字,大约阅读时间需要 3 分钟。

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.

 

1 public class Solution { 2     public int MinimumTotal(IList
> triangle) { 3 int count = triangle.Count; 4 5 if (count == 0) return 0; 6 7 var dp = new int[count + 1]; 8 9 for (int i = count - 1; i >= 0; i--)10 {11 for (int j = 0; j < triangle[i].Count; j++)12 {13 dp[j] = triangle[i][j] + Math.Min(dp[j], dp[j + 1]);14 }15 }16 17 return dp[0]; 18 }19 }

 

转载于:https://www.cnblogs.com/liangmou/p/7864924.html

你可能感兴趣的文章
IOS中UIView的响应事件,属性和方法
查看>>
C#解决方案生成工具(2)
查看>>
IHttpHandler处理请求api
查看>>
G450 Ubuntu14 无线网卡解决
查看>>
6.1.2 html
查看>>
Java集合框架--List、Set、Map
查看>>
T-SQL查询进阶-10分钟理解游标
查看>>
上传word里的图片到windows服务器[目前仅完全支持IE,公测]修正下载地址,要的快点,晚了,我就收了...
查看>>
poj 2502(floyd)
查看>>
Maven项目的坐标GroupId和ArtifactId
查看>>
scala foldLeft foldRight 简写
查看>>
MYSQL数据库备份还原
查看>>
微信开发笔记
查看>>
714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
查看>>
拓展编辑器(十九)_拓展全局自定义快捷键
查看>>
【微信小程序】自定义模态框实例
查看>>
ztree实现根节点单击事件,显示节点信息
查看>>
实现文字图片垂直居中的总结性方法
查看>>
洛谷P2002 消息扩散
查看>>
回归起点
查看>>