Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 8, 2026 12:27
Show Gist options
  • Select an option

  • Save Ephraimiyanda/f19a485aed8d24af4804c0110c82523e to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/f19a485aed8d24af4804c0110c82523e to your computer and use it in GitHub Desktop.
House Robber

Question

Approach

to get the max profit i check if robbing a house is better than skipping while folllowing the adjacent house rule

Complexity

  • Time complexity:O(N)

  • Space complexity:O(1)

Code

function rob(nums: number[]): number {
    let prev1 = 0 
    let prev2 = 0 

    for (let num of nums) {
        let temp = prev1
        prev1 = Math.max(prev1, prev2 + num)
        prev2 = temp
    }

    return prev1
}
scrnli_bcBWsiPhgPMNJ3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment