Java for 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
[
[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).
解题思路:
DP问题,用一个dp数组滚动下来即可,JAVA实现如下:
public int minimumTotal(List<List<Integer>> triangle) {
int[] dp = new int[triangle.size()];
dp[0]=triangle.get(0).get(0);
if(triangle.size()>=2){
dp[1]=triangle.get(1).get(1)+dp[0];
dp[0]+=triangle.get(1).get(0);
}
for (int i=2;i<triangle.size();i++) {
int left = dp[0];
int right = dp[1];
dp[0] += triangle.get(i).get(0);
for (int j = 1; j <= i - 1; j++) {
dp[j] = triangle.get(i).get(j) + Math.min(left, right);
left = right;
right = dp[j + 1];
}
dp[i] = left + triangle.get(i).get(i);
left = dp[0];
right = dp[1];
}
for (int i = 0; i < dp.length - 1; i++)
if (dp[i] < dp[i + 1])
dp[i + 1] = dp[i];
return dp[dp.length - 1];
}
Java for LeetCode 120 Triangle的更多相关文章
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Java实现 LeetCode 120 三角形最小路径和
120. 三角形最小路径和 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode 120]triangle 空间O(n)算法
1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- [leetcode] 120. Triangle (Medium)
原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
随机推荐
- HTTP Range - [Web开发]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://minms.blogbus.com/logs/39569593.html 所谓 Range,是在 HTTP/1.1(htt ...
- 常用 linux操作
查看libreoffice进程 ps -ef | grep libreoffice
- linux yum 安装软件
概括了部分常用的yum命令包括: 自动搜索最快镜像插件:yum install yum-fastestmirror安装yum图形窗口插件:yum install yumex 1 安装yum insta ...
- Linux学习之十-Linux系统时间
Linux系统时间 1.date命令用于查看以及修改Linux系统的时间,关于date命令的详细帮助文档如下 [root@localhost ~]# date --help Usage: date [ ...
- 【温故知新】——HTML基础重要知识点复习
前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 一.HTML快速入门(重点) 1.HTML概述 1.什么是HTML HTML : Hyper Text Markup Lan ...
- 使用yum方式在centOS上安装mysql
1.操作系统及MySQL版本 1.1 操作系统版本 CentOS release 6.5 (Final) 1.2 MySQL版本 mysql-5.1.73-3.el6_5.x86_64mysql-li ...
- 自定义序列化4 (MFC调用C#的.dll)
CLR:CLR常用简写词语,CLR是公共语言运行时,Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集),并保证应用和底层操作系 ...
- RelativeLayout布局(仅在RelativeLayout中有效)
在父亲布局的相对位置 android:layout_alignParentLeft="true" //在布局左边 android:layout_alignParentRig ...
- Build Your Hexo Blog (On Github)
超简单,比jekyll好多了! 看个Demo http://kevinjmh.github.io/ 了解Hexo Hexo是一个由Node.js驱动的,简单.快速.强大的Blog框架.可以快速的生成静 ...
- iOS UITableViewDelegate && UITableViewDataSource 执行顺序
#pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableV ...