You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 House Robber 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
Solution - 1
classSolution {
introb(List<int> nums) {
//max money can get if rob current houseint rob =0;
//max money can get if not rob current houseint notRob =0;
for (int i =0; i < nums.length; i++) {
//if rob current value, previous house must not be robbedint curRob = notRob + nums[i];
//if not rob ith house, take the max value of robbed (i-1)th house and not rob (i-1)th house
notRob =max(notRob, rob);
rob = curRob;
}
returnmax(rob, notRob);
}
}
Solution - 2
classSolution {
introb(List<int> nums) {
int previous =0;
int last =0;
for (int current in nums) {
last =max(previous + current, previous = last);
}
return last;
}
}