-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
committed
Sep 19, 2022
1 parent
24447a9
commit 2592f99
Showing
1 changed file
with
16 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution: | ||
def deleteAndEarn(self, nums: List[int]) -> int: | ||
maxVal = max(nums) | ||
total = [0] * (maxVal + 1) | ||
for val in nums: | ||
total[val] += val # 索引对应nums的值,值对应nums里值的和 | ||
# 每个房间有一定现金 | ||
def rob(nums: List[int]) -> int: | ||
dp = [0] * len(nums) | ||
dp[0] = nums[0] | ||
dp[1] = max(nums[0], nums[1]) | ||
for i in range(2, len(nums)): | ||
dp[i] = max(dp[i-2]+nums[i], dp[i-1]) | ||
return dp[-1] | ||
|
||
return rob(total) |