[Algorithm] Largest sum of non-adjacent numbers

Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.

For example, [2, 4, 6, 2, 5] should return 13, since we pick 26, and 5[5, 1, 1, 5] should return 10, since we pick 5 and 5.

Follow-up: Can you do this in O(N) time and constant space

 

How to think?

Always start from make few assumption / examples to see the parttens:

For example:

[5,1,1,5]

Start from i = 0: max sum can be Math.max(5, 0)

// memo: [5]

Then i = 1: max sum can be Math.max(memo[0], arr[1]), which is memo[1] = Math.max(5, 1) ---> 5

// memo :: [5, 5]

Then i = 2: max sum can be Math.max(memo[i - 1], arr[i] + memo[i - 2]), which is memo[2] = Math.max(5, 1 +5) --> 6:

// memo :: [5, 5, 6]

Then i = 3, should follow i = 2 partten:

// memo :: [5, 5, 6, 10]

 

So now, we got our partten:

for i = 2 to length
    memo[i] = Math.max(memo[i - 1], memo[i - 2] + arr[i])

 

Code:

function maxNonAdjSum(arr) {
  const memo = new Array(arr.length).fill(0);
  memo[0] = Math.max(0, arr[0]);
  memo[1] = Math.max(arr[1], memo[0]);

  for (let i = 2; i < arr.length; i++) {
    memo[i] = Math.max(memo[i-1], arr[i] + memo[i - 2]);
  }

  return memo;
}

console.log(maxNonAdjSum([2, 4, 6, 2, 5])); // 13
console.log(maxNonAdjSum([5, 1, 1, 5])); // 10 
console.log(maxNonAdjSum([2, 4, 6, 2, 5, -3, 4, 0])); // 17

 

To improve it furuther for space, we don't really need to keep 'memo' as a whole array, we just need to remember 3 values:

// [max_inc, max_not_inc, max]

And:

max = Math.max(max + max_inc, max_not_inc)

 

Cdoe:

function maxNonAdjSum2(arr) {
  let max_inc = Math.max(0, arr[0]);
  let max_not_inc = Math.max(arr[1], max_inc);

  let max = max_inc
  for (let i = 2; i < arr.length; i++) {
    max = Math.max(arr[i] + max_inc, max_not_inc);
    max_inc = max_not_inc;
    max_not_inc = max;
  }

  return max;
}

console.log(maxNonAdjSum2([2, 4, 6, 2, 5])); // 13
console.log(maxNonAdjSum2([5, 1, 1, 5])); // 10
console.log(maxNonAdjSum2([2, 4, 6, 2, 5, -3, 4, 0])); // 17

 

转载于:https://www.cnblogs.com/Answer1215/p/10554290.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值