Skip to content

Files

Latest commit

Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
and
Zanger67/leetcode
Mar 16, 2025
03398f8 · Mar 16, 2025

History

History
46 lines (32 loc) · 1.02 KB

_213. House Robber II.md

File metadata and controls

46 lines (32 loc) · 1.02 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : October 24, 2024

Last updated : October 24, 2024


Related Topics : Array, Dynamic Programming

Acceptance Rate : 43.16 %


Solutions

Python

class Solution:
    def rob(self, nums: List[int]) -> int:
        if not nums :
            return 0
        if len(nums) <= 2 :
            return max(nums)

        dp1 = nums[1:]
        dp2 = nums[:-1]
        dp1[1] = max(dp1[1], dp1[0])
        dp2[1] = max(dp2[1], dp2[0])

        for i in range(2, len(dp1)) :
            dp1[i] = max(dp1[i - 2] + dp1[i], dp1[i - 1])
            dp2[i] = max(dp2[i - 2] + dp2[i], dp2[i - 1])

        return max(dp1[-1], dp2[-1])