`
bcyy
  • 浏览: 1823761 次
文章分类
社区版块
存档分类
最新评论

Triangle -- LeetCode

 
阅读更多
原题链接:http://oj.leetcode.com/problems/triangle/
这是一道动态规划的题目,求一个三角形二维数组从顶到低端的最小路径和。思路是维护到某一个元素的最小路径和,那么在某一个元素i,j的最小路径和就是它上层对应的相邻两个元素的最小路径和加上自己的值,递推式是sum[i][j]=min(sum[i-1][j-1],sum[i-1][j])+triangle[i][j]。最后扫描一遍最后一层的路径和,取出最小的即可。每个元素需要维护一次,总共有1+2+...+n=n*(n+1)/2个元素,所以时间复杂度是O(n^2)。而空间上每次只需维护一层即可(因为当前层只用到上一层的元素),所以空间复杂度是O(n)。代码如下:
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
    if(triangle == null || triangle.size() == 0)
        return 0;
    if(triangle.size()==1)
        return triangle.get(0).get(0);
    int[] sums = new int[triangle.size()];
    sums[0] = triangle.get(0).get(0);
    for(int i=1;i<triangle.size();i++)
    {
        sums[i] = sums[i-1]+triangle.get(i).get(i);
        for(int j=i-1;j>=1;j--)
        {
            sums[j] = (sums[j]<sums[j-1]?sums[j]:sums[j-1]) + triangle.get(i).get(j);
        }
        sums[0] = sums[0]+triangle.get(i).get(0);
        
    }
    int minimum = sums[0];
    for(int i=1;i<sums.length;i++)
    {
        if(sums[i]<minimum)
        {
            minimum = sums[i];
        }
    }
    return minimum;
}
上述代码实现时要注意每层第一二个元素特殊处理一下。这道题是不错的动态规划题目,模型简单,比较适合在电面中出现。
分享到:
评论

相关推荐

    leetcodelintcode差异-leetcode-python:leetcode-python

    leetcode-python 九章算法基础班 二分 题目 地址 153. Find Minimum in Rotated Sorted Array 双指针 题目 Solution Tag LintCode 604. Window Sum LeetCode 283. Moves Zeroes Array、Two Pointers Array、Two ...

    leetcode双人赛-leetcode-solution:没事可做的时候,就来刷刷题吧

    leetcode-solution 闲暇之余,刷一下题,弥补数据结构和算法的短板 概述 之前写过一个 leetcode 的一点题解,不过后来就断了,现在重新开个项目:party_popper::party_popper::party_popper: 项目计划 规则 为了尽...

    leetcode卡-leetcode_python:leetcode_python

    Triangle 2020-01-15 213 House Robberll -变种 198 337 2020-01-16 139 单词拆分 2020-01-20 104 树 -变种 111 2020-01-21 129 Sum root to leaf numbers 2020-01-22 226 翻转二叉树 2020-01-23 95 不同的二叉搜索...

    圆和矩形是否重叠leetcode-leetcode_solutions:leetcode_solutions

    圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -&gt; 使用哈希表避免遍历列表448.查找数组中消失的所有数字-&gt; 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of Binary Tree 栈 evaluate-reverse-polish-notation 链表 sort-list 排序 insertion-sort-list 树 binary-tree...

    leetcode2sumc-Leetcode-2020:刷刷Leetcode并作纪录

    leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection Question) 准备方式 分析tag的热门标签,熟悉各个标签解题的思路(解决该标签全部的easy和medium为主),再...

    LeetCode-Triangle

    LeetCode-Triangle

    2sumleetcode-leetcode:leetcode

    2sum leetcode leetcode 文件和描述 removeelement.cpp,删除特定元素 ...triangle.cpp,动态方法从三角形中找到从上到下的最小路径总和 maxsubarray.cpp,动态方法从数组中找到连续子数组的最大和

    javalruleetcode-leetcode-python:leetcode问题的Python解决方案

    leetcode 刷题笔记 记录一些刷题细节,很惭愧只做了一点微小的工作 4.13 162题. Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress ...Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar

    leetcode答案-leetcode:每日三题

    Triangle Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space (size that is greater or equal to m + n)to hold additional elements ...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by JavaScript and TypeScript. easy 66.加一 (Plus One) 67.二进制求和 (Add Binary) 69.x 的平方根 (Sqrt(x)) 70.爬楼梯 ...

    javalruleetcode-leetcode:leetcode

    java lru leetcode leetcode HashMap问题 滑动窗口问题 ...Math.min(dp[i-1][j-1],dp[i-1][j]+triangle[i][j]) dp[i] += dp[i-j]*dp[j-1]) dp[i][j]=dp[i-1][j]+dp[i][j-1]) dp[i]=Math.max(dp[i-1],dp[i-2

    leetcode-7:leetcode 题解,更新中

    leetcode========leetcode 题解,更新中.提供Java和 Python 版本的solution为了让更多的同学了解并应用Leetcode,...ii/简单数学:http://oj.leetcode.com/problems/pascals-triangle/http://oj.leetcode.com/proble

    DP-LeetCode1143. 最长公共子序列(Python)

    # Modify the original triangle, bottom-up def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """ if not triangle: return for i in range(len(triangle) - 2, -

    leetcode添加元素使和等于-leetCode:leetcode

    leetcode添加元素使和等于 leetCode 递归 95 能够想到用递归左右产生子树的方法,但是程序就是写不出来,主要在于对于一个root i, 要实现左右子树的所有情况,左右子树是独立的, 添加两层循环,把左右子树的各种...

    leetcode卡-LeetCode:我使用JavaScript的LeetCode解决方案

    Triangle (杨辉三角) 124 二叉树最大路径和 136 x ^ x = 0 169 Majority Vote Algorithm (最大投票数算法) 240 检索二阶矩阵 189 数组操作的时间复杂度比较 206 反转单向链表 226 反转二叉树 459 重复子字符串模式...

    gasstationleetcode-LeetCode_Practice:我的LeetCode练习从2020年开始

    leetcode 力扣_实践 标签: leetcode 我的 LeetCode 练习从 2020 年开始 前言 尝试将 Leetcode 作为我的日常练习。 将更新带有解释的解决方案。 大批 27_Remove_element 26_Remove_Duplicates 80_Remove_Duplicates_...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

Global site tag (gtag.js) - Google Analytics