House Robber | Leetcode 198 | Live coding session 🔥🔥🔥🔥🔥 | Dynamic Programming

preview_player
Показать описание
Here is the solution to "House Robber" leetcode question. Hope you have a great time going through it.

1) 0:00 Explaining the problem out loud
2) 1:50 Algorithm walkthrough
3) 6:00 Coding

For discussion/feedback

PS : Please increase the speed to 1.25X
Рекомендации по теме
Комментарии
Автор

Great thanks.
Your knowledge, skills and effort for common good are appreciated

mrrishiraj
Автор

Code can be bit simplified
dp[0] = nums[0]
dp[1]=Math.max(dp[0], dp[1])
for(i=2;i<nums.length;i++){
dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i])
}
return dp[nums.length-1]


Space complexity can also be reduced since we depends on only last two states
Instead of array can use two variables snd update them

sreejith
Автор

can we return Math.max(dp[n-1], dp[n-2]) instead of using max variable ?

ojasvichaudhary