publicclassSolution{ publicintclimbStairs(int n){ if (n == 1) { return1; } int first = 1; int second = 2; for (int i = 3; i <= n; i++) { int third = first + second; first = second; second = third; } return second; } }
classSolution(object): defmaxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ ifnot nums: return0 if len(nums) == 1: return nums[0] dp = [float('-inf') for _ in range(len(nums))]
dp[0] = nums[0] res = nums[0] for each in range(1,len(nums)): dp[each] = max(dp[each-1]+nums[each], nums[each]) res = max(res, dp[each]) #print dp return res
从上一段分析可以看出,dp状态只与上一个状态有关,从而可以简化成变量来储存dp[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution(object): defmaxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ ifnot nums: return0 if len(nums) == 1: return nums[0] prevMax = nums[0] res = nums[0] for each in range(1,len(nums)): prevMax = max(prevMax+nums[each], nums[each]) res = max(res, prevMax) #print dp return res
300. Longest Increasing Subsequence
找出Max 这道题有点不一样的地方是最后的结果有可能是任意一个位置,所以不是简单的return dp[-1]而是max(dp) dp[i] = 1 + max(dp[j]) j < i and A[i] > A[j]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): deflengthOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ ifnot nums: return0 dp = [1] * (len(nums) + 1) for i in range(len(nums)): for j in range(i): if nums[j] < nums[i]: dp[i] = max(dp[i], dp[j]+1) return max(dp)
139.Word Break
寻求解的存在性 和上一题有点像,dp[i] 为当前字符满足之前的字符在字典里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution(object): defwordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str] :rtype: bool """ ifnot s: returnFalse # dp保存dp【i】i之前的最少字符串 dp = [False] * (len(s) + 1) dp[0] = True # 本身的循环,对字符串,在内层循环中需要使用i for i in range(1, len(s)+1): for j in range(i): if dp[j] and s[j:i] in wordDict: dp[i] = True# 因为j~i是一个回文字符串 return dp[-1]
classSolution(object): deffindNumberOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ ifnot nums: return0 dp = [1for _ in range(len(nums))] dc = [1for _ in range(len(nums))] for i in range(len(nums)): for j in range(i):
if nums[i] > nums[j]: if dp[i] < dp[j] + 1: dp[i] = dp[j] + 1 dc[i] = dc[j] elif dp[i] == dp[j] + 1: dc[i] += dc[j] # we have multiple same length LIS, we need to add them res = 0
for index, value in enumerate(dp): if value == max(dp): res += dc[index] return res
309. Best Time to Buy and Sell Stock with Cooldown
classSolution(object): defuniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ ifnot m ornot n: return0 dp = [[1for _ in range(n)] for _ in range(m)] for i in range(1,m): for j in range(1,n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]
classSolution(object): defuniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ dp = [1for _ in range(m)] for i in range(1,n): for j in range(1,m): dp[j] += dp[j-1] return dp[-1]
classSolution(object): defuniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ m = len(obstacleGrid) n = len(obstacleGrid[0]) dp = [[0] * n] * m for i in range(m): for j in range(n): if obstacleGrid[i][j] == 1: dp[i][j] = 0 elif i == 0and j == 0: dp[i][j] = 1 elif i == 0: dp[i][j] = dp[i][j-1] elif j == 0: dp[i][j] = dp[i-1][j] else: dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]
64. Minimum Path Sum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution(object): defminPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ m = len(grid) n = len(grid[0]) dp = [[0for _ in range(n)] for _ in range(m)] dp[0][0] = grid[0][0] for i in range(1,m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1,n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1,m): for j in range(1,n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] #print dp return dp[-1][-1]
classSolution(object): defminPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ m = len(grid) n = len(grid[0]) dp = [0for _ in range(m)] dp[0] = grid[0][0] for i in range(1,m): dp[i] = dp[i-1] + grid[i][0]
for j in range(1,n): dp[0] = dp[0] + grid[0][j] for i in range(1,m): dp[i] = min(dp[i], dp[i-1]) + grid[i][j] #print dp return dp[-1]
classSolution(object): defcalculateMinimumHP(self, dungeon): """ :type dungeon: List[List[int]] :rtype: int """ m = len(dungeon) n = len(dungeon[0]) dp = [[0for _ in range(n)] for _ in range(m)]
for i in range(m-1,-1,-1): for j in range(n-1,-1,-1): if i == m-1and j == n-1: dp[i][j] = max(1, 1 - dungeon[i][j]) elif i == m-1: dp[i][j] = max(1, dp[i][j+1] - dungeon[i][j]) elif j == n-1: dp[i][j] = max(1, dp[i+1][j] - dungeon[i][j]) else: dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]) return dp[0][0]
classSolution(object): defmaxA(self, N): """ :type N: int :rtype: int """ if N <= 6: return N dp = [i for i in range(N+1)] for i in range(7,N+1): for j in range(1,i-2): dp[i] = max(dp[i], dp[j] * (i-j-1)) return dp[-1]
def__init__(self, matrix): """ :type matrix: List[List[int]] """ ifnot matrix: return m = len(matrix) n = len(matrix[0]) # deal with zero row and column self.dp = [[0for _ in range(n+1)] for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): self.dp[i][j] = matrix[i-1][j-1] + self.dp[i-1][j] + self.dp[i][j-1] - self.dp[i-1][j-1]
defsumRegion(self, row1, col1, row2, col2): """ :type row1: int :type col1: int :type row2: int :type col2: int :rtype: int """ return self.dp[row2+1][col2+1] - self.dp[row2+1][col1]- self.dp[row1][col2+1] + self.dp[row1][col1]
# Your NumMatrix object will be instantiated and called as such: # obj = NumMatrix(matrix) # param_1 = obj.sumRegion(row1,col1,row2,col2)
classSolution(object): defmaximalSquare(self, matrix): """ :type matrix: List[List[str]] :rtype: int """ ifnot matrix: return0 res = 0 m = len(matrix) n = len(matrix[0]) dp = [[0for _ in range(n+1)] for _ in range(m+1)] for i in range(1,m+1): for j in range(1, n+1): if matrix[i-1][j-1] == '1':
classSolution(object): defminDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ l1 = len(word1)+1 l2 = len(word2) + 1 dp = [[0for _ in range(l2)] for _ in range(l1)]
for i in range(l1): dp[i][0] = i for j in range(l2): dp[0][j] = j for i in range(1,l1): for j in range(1,l2): if word1[i-1] == word2[j-1]: dp[i][j] = dp[i-1][j-1] else: dp[i][j] = min(dp[i][j-1],dp[i-1][j], dp[i-1][j-1]) + 1 #print dp return dp[-1][-1]