[LeetCode]题解(python):120 Triangle

时间:2021-08-20 12:14:40

题目来源


https://leetcode.com/problems/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.


题意分析


Input:一个三角矩阵

Output:最短路径

Conditions:使用O(n)空间


题目思路


首先初始化一个n大小的array,然后,初始化array为0,array[0]就是triangle的第一行的值,然后遍历剩余行,不断更新array,注意要从后往前遍历,否则会覆盖前面值。


AC代码(Python)

 class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
if len(triangle) == 0: return 0
array = [0 for i in range(len(triangle))]
array[0] = triangle[0][0]
for i in range(1,len(triangle)):
for j in range(len(triangle[i]) - 1, -1, -1):
if j == len(triangle[i]) - 1:
array[j] = array[j - 1] + triangle[i][j]
elif j == 0:
array[j] = array[j] + + triangle[i][j]
else:
array[j] = min(array[j-1], array[j]) + triangle[i][j]
return min(array)

[LeetCode]题解(python):120 Triangle的更多相关文章

  1. [LeetCode 题解]: Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

  3. 【LeetCode】120&period; Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. &lbrack;LeetCode 题解&rsqb;: Triangle

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a tr ...

  5. leetcode 118&period; Pascal&&num;39&semi;s Triangle 、119&period; Pascal&&num;39&semi;s Triangle II 、120&period; Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  6. 【LeetCode题解】7&lowbar;反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  7. 【LeetCode题解】3&lowbar;无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  8. 【LeetCode题解】225&lowbar;用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  9. 【LeetCode题解】232&lowbar;用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  10. 【LeetCode题解】844&lowbar;比较含退格的字符串(Backspace-String-Compare)

    目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...

随机推荐

  1. JS验证只能输入数字,数字和字母等的正则表达式

    JS判断只能是数字和小数点 0.不能输入中文1)<input onpaste="return false;" type="text" name=&quot ...

  2. angularjs指令系统系列课程(4):作用域Scope

    指令的scope对象是一个很重要,很复杂的对象,我们这一节作为重点讲解 可取值: 1.false(默认), 2.true, 3.{}(object) 1.false:默认值,不创建新的作用域 2.tr ...

  3. 关于showModalDialog&lpar;&rpar;对话框点击按钮弹出新页面的问题

    页面a.aspx上,单击按钮a,走脚本,弹出showModalDialog("b.aspx",....) 在b.aspx上有个服务器控件按钮b,单击按钮,更新数据后,会弹出一个新的 ...

  4. Easyui 中的placeholder属性

    在 easyui有文档中,没注意还真找不到placeholder属性,因为在属性只在searchbox中提到了, <input id="ss" class="eas ...

  5. 转自邓凡平 《深入理解Android:Wi-Fi,NFC和GPS》章节连载&lbrack;节选&rsqb;--第七章 深入理解Wi-Fi P2P部分节选

    本章主要内容: 介绍Wi-Fi P2P相关知识: 介绍Android中WifiP2pService.wpa_supplicant的相关代码. 7.1  概述 承接第6章介绍的WSC,本章将继续介绍Wi ...

  6. Objective-C中instancetype和id的区别

    要区分instancetype和id,首先要弄清楚什么是关联返回类型(Related Result Type). 关联返回类型即一个方法的返回类型就是调用这个方法的调用者的类型.具有下列条件的方法具有 ...

  7. 第11章 享元模式(Flyweight Pattern)

    原文 第11章 享元模式(Flyweight Pattern) 概述:   面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情况下,对象的数量可能会太多,从而导致了运行时 ...

  8. iOS Push详述,了解一下?

    WeTest 导读 本文主要对iOS Push的在线push.本地push及离线(远程)push进行梳理,介绍了相关逻辑,测试时要注意的要点以及相关工具.小小的Push背后蕴藏着大大的逻辑! Push ...

  9. 洛谷P3690 Link Cut Tree &lpar;模板&rpar;

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  10. POJ 1236 Network of Schools (Tarjan)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22745   Accepted: 89 ...