美文网首页
动态规划-120题triangle

动态规划-120题triangle

作者: kason_zhang | 来源:发表于2020-04-05 23:56 被阅读0次

动态规划:
将原问题拆解成若干子问题,同时保存子问题的答案,使得每个子问题只求解一次,最终获得原问题的答案。

image.png

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

递归解法:

public int minimumTotal(List<List<Integer>> triangle) {
        int min = Integer.MAX_VALUE;
        if (triangle.size() == 1) {
            return triangle.get(0).get(0);
        }
        for (int i = 0; i < triangle.size(); i++) {
            int temp = minValue(triangle.size()-1, i, triangle);
            if (min > temp){
                min = temp;
            }
        }
        return min;
    }
/**
     * 递归解法
     * @param row
     * @param col
     * @param triangle
     * @return
     */
    public int minValue(int row, int col, List<List<Integer>> triangle) {
        if (row == 0) {
            return triangle.get(0).get(0);
        }
        if (col == 0) {
            return minValue(row-1, 0, triangle) + triangle.get(row).get(col);
        }
        if (col == triangle.get(row).size() - 1) {
            return minValue(row-1, triangle.get(row).size()-2, triangle)+ triangle.get(row).get(col);
        }
        return Math.min(minValue(row-1, col-1, triangle), minValue(row-1, col, triangle)) + triangle.get(row).get(col);
    }

动态规划解法

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        // 存储走到第i行i列的最小值
        int[][] minValues = new int[triangle.size() ][triangle.size()];
        minValues[0][0] = triangle.get(0).get(0);
        if (triangle.size() == 1) {
            return  minValues[0][0];
        }
        for (int row = 1; row < triangle.size(); row++) {
            for (int col = 0; col <= row; col++) {
                if (col == 0) {
                    minValues[row][col] = minValues[row-1][col] + triangle.get(row).get(col);
                } else if (col == row) {
                    minValues[row][col] = minValues[row-1][col-1] + triangle.get(row).get(col);
                } else {
                    minValues[row][col] = Math.min(minValues[row-1][col-1], minValues[row-1][col])+ triangle.get(row).get(col);
                }
            }
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < triangle.size(); i++) {
            min =  Math.min(min,minValues[triangle.size()-1][i]);
        }
        return min;
    }
}

相关文章

  • 动态规划-120题triangle

    动态规划:将原问题拆解成若干子问题,同时保存子问题的答案,使得每个子问题只求解一次,最终获得原问题的答案。 Giv...

  • Leetcode-120Triangle

    120. Triangle Given a triangle, find the minimum path sum...

  • 120.Triangle(week10)

    120.Triangle 题目描述 Given a triangle, find the minimum path...

  • LeetCode 120. Triangle

    10-16 LeetCode 120. Triangle Triangle Description Given a...

  • LintCode 109. Triangle

    原题 LintCode 109. Triangle Description Given a triangle, f...

  • Triangle

    //120. TriangleGiven a triangle, find the minimum path su...

  • 动态规划之数列的最大和

    Super Jumping! Jumping! Jumping!该题作为动态规划的讲解例子,首先需要明白动态规划问...

  • 编辑距离 Edit Distance

    简介请点击leetcode 这里 这道题是动态规划。动态规划的核心思想是缓存。解决动态规划的主要方法是,找出状态转...

  • 120. Triangle

    top to down的方案状态转移: f(x,y) = min(f(x-1, y-1), f(x-1, y)) ...

  • 120. Triangle

    Given a triangle, find the minimum path sum from top to b...

网友评论

      本文标题:动态规划-120题triangle

      本文链接:https://www.haomeiwen.com/subject/hcjuphtx.html