From 996c97acdced27bee443251eed53b096d8f8c460 Mon Sep 17 00:00:00 2001 From: rixant Date: Fri, 27 Jan 2023 21:50:31 -0500 Subject: [PATCH] python: Create delete and earn. --- python/0740-delete-and-earn.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 python/0740-delete-and-earn.py diff --git a/python/0740-delete-and-earn.py b/python/0740-delete-and-earn.py new file mode 100644 index 000000000..d33e67221 --- /dev/null +++ b/python/0740-delete-and-earn.py @@ -0,0 +1,23 @@ +# House Robber Style +# Time Complexity O(n) +# Space Complexity O(n) +class Solution(object): + def deleteAndEarn(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + + upperLimit = max(nums) + 1 + store = [0] * upperLimit + + for num in nums: + store[num] += num + + dp = [0] * upperLimit + + dp[1] = 1 * store[1] + for i in range(2, upperLimit): + dp[i] = max(dp[i - 2] + store[i], dp[i - 1]) + + return dp[-1] \ No newline at end of file