198. House Robber
Approach 1: Keeping track of amount robbed using DP
Here we keep track of the max sum from position 0 to i
in dp[i]
. This is calculated as follows dp[i] = nums[i] + max(dp[i-2], dp[i-3])
Moreover the initial conditions are dp[0]
, dp[1]
and dp[2]
which are initalised as nums[0]
, nums[1]
and nums[0] + nums[2]
respectively.
In the end we only have to check the last two entries to see which one is greater and return the larger one.
Time Complexity: O(N)
Space Complexity: O(N)
Last updated