vault backup: 2022-07-20 22:51:09
This commit is contained in:
parent
4727c3321a
commit
cc1da4d25c
|
@ -145,3 +145,39 @@ public:
|
|||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
#### Stage 4:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int rob(vector<int> &nums) {
|
||||
// iterative, from the buttom up
|
||||
// We can utilize the hash map feature
|
||||
|
||||
int size = nums.size();
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
} else if (size == 1) {
|
||||
return nums[0];
|
||||
}
|
||||
|
||||
int prev1, prev2, next; // next: i + 1
|
||||
|
||||
int curLoc = 1;
|
||||
prev1 = nums[0]; // i - 1
|
||||
prev2 = max(nums[0], nums[1]); // i
|
||||
next = prev2;
|
||||
while (curLoc < size - 1) {
|
||||
// cache[curLoc + 1] = max(cache[curLoc - 1] + nums[curLoc + 1] ,
|
||||
// cache[curLoc]);
|
||||
next = max(prev1 + nums[curLoc + 1], prev2);
|
||||
prev1 = prev2;
|
||||
prev2 = next;
|
||||
curLoc++;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in a new issue