动态规划_leetcode120(经典的梯形模式)
Posted AceKo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划_leetcode120(经典的梯形模式)相关的知识,希望对你有一定的参考价值。
class Solution1(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
self.dp(triangle)
# i
# i , i+1
def dp(self,triangle):
length = len(triangle)
memo = [-1 for i in range(length)]
for i in range(length-1,-1,-1):
if i == length-1:
for j in range(len(triangle[i])):
memo[j] = triangle[i][j]
else:
temp = memo[0:]
for j in range(len(triangle[i])):
memo[j] = min(temp[j],temp[j+1])+triangle[i][j]
print memo[0]
return memo[0]
class Solution2(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
pass
s = Solution1()
t = [
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
s.minimumTotal(t)
以上是关于动态规划_leetcode120(经典的梯形模式)的主要内容,如果未能解决你的问题,请参考以下文章