leetcode-27双周赛-5411-摘樱桃Ⅱ
题目描述:
方法:动态规划 自底向上:
class Solution: def cherryPickup(self, A): R, C=len(A), len(A[0]) from functools import lru_cache @lru_cache(None) def dp(r,c1,c2): if r == R: return 0 ans = 0 for d1 in (-1, 0, 1): c1new = c1 + d1 if 0 <= c1new < C: for d2 in (-1, 0, 1): c2new = c2 + d2 if 0 <= c2new < C: ans = max(ans, dp(r+1, c1new, c2new)) base = A[r][c1] if c1 != c2: base += A[r][c2] return ans + base return dp(0,0,C-1)