Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 198 · 1D / Fibonacci

House Robber

LeetCode 198 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

best(i) = max(nums[i] + best(i-2), best(i-1)).

మధ్యస్థంArrayDynamic Programming
LeetCode లో తెరవండి ↗

నేను House Robber (LeetCode 198) ను ఇలా పరిష్కరించాను — 1D / Fibonacci ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

At each house you make one decision: rob it (and you must have skipped the previous one) or skip it (and keep whatever was best up to the previous house). The best haul up to house i only depends on the best up to i-1 and i-2.

Approach

  1. 1

    State: best(i) = most money from houses 0..i.

  2. 2

    Base cases: best(0) = nums[0]; best(1) = max(nums[0], nums[1]).

  3. 3

    Rob choice: nums[i] + best(i-2). Skip choice: best(i-1).

  4. 4

    Take the max of the two and move right; the answer is best(n-1).

  5. 5

    Guard the single-house input before touching nums[1].

నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు

1
Recursion + memoO(n) time · O(n) space

lru_cache dfs(index) choosing rob vs don't-rob.

2
TabulationAcceptedO(n) time · O(n) space

dp[index] = max(rob, dontrob) filled left to right.

Recursion + memoPythonO(n) time · O(n) space
from functools import lru_cache
class Solution:
    def rob(self, nums: List[int]) -> int:
        #eithter i can rob and go to index-2
        #or skip and go to index-1
        @lru_cache(maxsize=None)
        def dfs(index):
            if index==0:
                return nums[0]
            if index==1: #two choice, either rob nums[1] or nums[0]
                return max(nums[0],nums[1])
            rob = nums[index]+dfs(index-2)
            dontrob = dfs(index-1)
            return max(rob,dontrob)
        return dfs(len(nums)-1)
TabulationPythonO(n) time · O(n) space
class Solution:
    def rob(self, nums: List[int]) -> int:
        #eithter i can rob and go to index-2
        #or skip and go to index-1
        if len(nums)==1:
            return nums[0]
        n = len(nums)
        dp=[float("-inf")]*n
        dp[0]=nums[0]
        dp[1]=max(nums[0],nums[1])
        for index in range(2,n):
            rob = nums[index]+dp[index-2]
            dontrob = dp[index-1]
            dp[index]=max(rob,dontrob)
        return dp[n-1]
స్టెప్-త్రూ లోడ్ అవుతోంది…

House Robber — తరచూ అడిగే ప్రశ్నలు

House Robber వెనుక ఉన్న intuition ఏమిటి?

At each house you make one decision: rob it (and you must have skipped the previous one) or skip it (and keep whatever was best up to the previous house). The best haul up to house i only depends on the best up to i-1 and i-2.

House Robber (LeetCode 198) ను ఎలా పరిష్కరించాలి?

1. State: best(i) = most money from houses 0..i. 2. Base cases: best(0) = nums[0]; best(1) = max(nums[0], nums[1]). 3. Rob choice: nums[i] + best(i-2). Skip choice: best(i-1). 4. Take the max of the two and move right; the answer is best(n-1). 5. Guard the single-house input before touching nums[1].

House Robber యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(n) (reducible to O(1) with two variables). (Accepted approach: Tabulation.)

House Robber లో సాధారణ పొరపాట్లు ఏమిటి?

A single house must return nums[0] before dp[1] is read. Greedy 'take every other house' fails on inputs like [2,1,1,2].

House Robber ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?

Recursion + memo (O(n) time · O(n) space): lru_cache dfs(index) choosing rob vs don't-rob. Tabulation (O(n) time · O(n) space): dp[index] = max(rob, dontrob) filled left to right.