@violangreg/

Leetcode

Nodejs

No description

fork
loading
Files
  • index.js
  • houseRobber.js
  • longestCommonPrefix.js
  • ramsomMagazine.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function(nums) {
    even_sum = 0
    odd_sum = 0

    for(i = 0; i < nums.length; i++){
      if(i%2 == 0) // even
        even_sum = Math.max(even_sum + nums[i], odd_sum)
      else
        odd_sum = Math.max(even_sum, odd_sum + nums[i])
        
      console.log("i: " + i + ", nums[i]: " + nums[i] + ", even_sum: " + even_sum + ", odd_sum: " + odd_sum)
    }
    return Math.max(even_sum, odd_sum)
};



console.log(rob([1, 50, 3, 4, 55, 1]));
node v10.16.0