Skip to content

Commit 583b82f

Browse files
committed
Time: 0 ms (100%), Space: 53.9 MB (42.7%) - LeetHub
source:310d3b57fce87aae5db97c347a82d5a6b5003d45
1 parent 7842afb commit 583b82f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function(nums) {
6+
const dp =[];
7+
dp[0] = nums[0];
8+
dp[1] = nums[1];
9+
dp[2] = nums[0]+nums[2];
10+
for(let i=3;i<nums.length;i++){
11+
dp[i]=Math.max(dp[i-2],dp[i-3])+nums[i];
12+
}
13+
14+
if(nums.length <2){
15+
return dp[nums.length-1]
16+
}
17+
return Math.max(dp[nums.length-1], dp[nums.length-2]);
18+
19+
20+
};

0 commit comments

Comments
 (0)