LeetCode: Triangle 解题报告

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).

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.

SOLUTION 1:

使用DFS 加记忆矩阵的解法.

mem[i][j]表示第i行第j列的解。它的解可以由下一行推出:mem[i][j] = mem[i+1][j] + mem[i+1][j+1]

 1 /*
 2     REC, SOL 1:
 3     */
 4     public int minimumTotal1(List<List<Integer>> triangle) {
 5         if (triangle == null || triangle.size() == 0) {
 6             return 0;
 7         }
 8
 9         int rows = triangle.size();
10         int[][] mem = new int[rows][rows];
11         for (int i = 0; i < rows; i++) {
12             for (int j = 0; j < rows; j++) {
13                 mem[i][j] = Integer.MAX_VALUE;
14             }
15         }
16
17         return dfs(triangle, 0, 0, mem);
18     }
19
20     public int dfs(List<List<Integer>> triangle, int row, int col, int[][] mem) {
21         if (mem[row][col] != Integer.MAX_VALUE) {
22             return mem[row][col];
23         }
24
25         if (row == triangle.size() - 1) {
26             mem[row][col] = triangle.get(row).get(col);
27         } else {
28             int left = dfs(triangle, row + 1, col, mem);
29             int right = dfs(triangle, row + 1, col + 1, mem);
30             mem[row][col] = triangle.get(row).get(col) + Math.min(left, right);
31         }
32
33         return mem[row][col];
34     }

SOLUTION 2:

ref: http://blog.csdn.net/imabluefish/article/details/38656211

动态规划的题目

我们可以轻松将上面的修改为DP.

并且,为了减少内存使用量,使用一维DP即可。

f[j] 表示下一行第j列某点到最后底部的最短值。因为我们只需要下一行的这个值,所以我们使用一行的DP memory即可完成任务。

第一步: 先计算出最后一排的最短值,实际上就是这一排本身的值。
第二步:From bottom to up, 每一层的最短值只需要把自身值加上,并且取下层的左右邻接点的最小值。

 1 /*
 2     DP, SOL 2:
 3     */
 4     public int minimumTotal(List<List<Integer>> triangle) {
 5         if (triangle == null || triangle.size() == 0) {
 6             return 0;
 7         }
 8
 9         int rows = triangle.size();
10         int[] D = new int[rows];
11
12         for (int i = rows - 1; i >= 0; i--) {
13             // 注意:边界条件是 j <= i
14             for (int j = 0; j <= i; j++) {
15                 if (i == rows - 1) {
16                     D[j] = triangle.get(i).get(j);
17                 } else {
18                     D[j] = triangle.get(i).get(j) + Math.min(D[j], D[j + 1]);
19                 }
20             }
21         }
22
23         return D[0];
24     }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinimumTotal.java

时间: 2024-08-05 10:49:54

LeetCode: Triangle 解题报告的相关文章

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

【LeetCode】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 

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

[LeetCode] 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