Grokking Dynamic Programming Patterns for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
Recursive / DP solution

Mohammed Dh Abbas

Oct 21, 2024

class Solution: def findMaxSteal(self, wealth): def dp(index, acc, skip, memo): if (index, acc, skip) in memo: return memo[(index, acc, skip)] if index == len(wealth): return acc if index > len(wealth): return 0 result = 0 if not skip: with_house = dp(index + 1, wealth[index] + acc, True, memo) without_house = dp(index + 1, acc, False, memo) result = max(with_house, without_house) else: result = dp(index + 1, acc, False, memo) memo[(index, acc, skip)] = result return result return dp(0, 0, False, {})

0

0

Comments
Comments

On this page