Skip to content

Commit a29d8be

Browse files
committed
Update house-robber.js
1 parent 2bc6716 commit a29d8be

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

leetcode/house-robber.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
var rob = function(nums) {
2222
var currMaxPV = 0, //当前最大值:上一轮循环中的最大值加上当前值
2323
lastRealMax = 0, //上一轮循环中的真正的最大值
24-
i = nums.length -1,
25-
tempLastMaxPV, //
26-
realLastMax;
24+
i = nums.length -1;
25+
2726
while ( 0 <= i) { // eg: [2,1,1,2]
28-
tempLastMaxPV = currMaxPV; //2 0 3
29-
realLastMax = lastRealMax; //0 2 2
30-
currMaxPV = realLastMax + nums[i];//1 3 4
31-
lastRealMax = tempLastMaxPV > realLastMax ? tempLastMaxPV :realLastMax;//2 2 3
32-
i--;
27+
var tempLastMaxPV = currMaxPV, //2 0 3
28+
realLastMax = lastRealMax; //0 2 2
29+
currMaxPV = realLastMax + nums[i];//1 3 4
30+
lastRealMax = Math.max(tempLastMaxPV, realLastMax);//2 2 3
31+
i--;
3332
}
34-
return currMaxPV > lastRealMax ? currMaxPV : lastRealMax;
33+
return Math.max(currMaxPV, lastRealMax); // 4
3534
};
35+

0 commit comments

Comments
 (0)