198. House Robber
Last updated
Last updated
/**
* Time complexity : O(N)
* Space complexity : O(1)
*/
class Solution {
public int rob(int[] nums) {
int prevToPrev = 0;
int prev = 0;
int max = 0;
for(int n: nums) {
max = Math.max(prev, prevToPrev+n);
prevToPrev = prev;
prev = max;
}
return max;
}
}/**
* Time complexity : O(N)
* Space complexity : O(1)
*/
class Solution {
public int rob(int[] nums) {
int prevMax = 0;
int currMax = 0;
for(int num: nums) {
int tmp = currMax;
currMax = Math.max(currMax, prevMax+num);
prevMax = tmp;
}
return currMax;
}
}