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:

  1. [5,1,1,5]

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

  1. // memo: [5]

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

  1. // 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:

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

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

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

So now, we got our partten:

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

Code:

  1. function maxNonAdjSum(arr) {
  2. const memo = new Array(arr.length).fill();
  3. memo[] = Math.max(, arr[]);
  4. memo[] = Math.max(arr[], memo[]);
  5.  
  6. for (let i = ; i < arr.length; i++) {
  7. memo[i] = Math.max(memo[i-], arr[i] + memo[i - ]);
  8. }
  9.  
  10. return memo;
  11. }
  12.  
  13. console.log(maxNonAdjSum([, , , , ])); //
  14. console.log(maxNonAdjSum([, , , ])); // 10
  15. console.log(maxNonAdjSum([, , , , , -, , ])); //

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:

  1. // [max_inc, max_not_inc, max]

And:

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

Cdoe:

  1. function maxNonAdjSum2(arr) {
  2. let max_inc = Math.max(, arr[]);
  3. let max_not_inc = Math.max(arr[], max_inc);
  4.  
  5. let max = max_inc
  6. for (let i = ; i < arr.length; i++) {
  7. max = Math.max(arr[i] + max_inc, max_not_inc);
  8. max_inc = max_not_inc;
  9. max_not_inc = max;
  10. }
  11.  
  12. return max;
  13. }
  14.  
  15. console.log(maxNonAdjSum2([, , , , ])); //
  16. console.log(maxNonAdjSum2([, , , ])); //
  17. console.log(maxNonAdjSum2([, , , , , -, , ])); //

[Algorithm] Largest sum of non-adjacent numbers的更多相关文章

  1. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  2. [Swift]LeetCode813. 最大平均值和的分组 | Largest Sum of Averages

    We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...

  3. LC 813. Largest Sum of Averages

    We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...

  4. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  6. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  7. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  9. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. hdu 4544 优先队列+贪心

    题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...

  2. webwork或Struts配置网站根路径的默认页面办法

    参考资料:http://www.iteye.com/problems/24028 查阅好多资料,关于webwork或Struts处理默认页面的方式,能否像spring MVC那样直接指定默认访问页面. ...

  3. python数据库操作——sqlite3模块

    # -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...

  4. jmeter用BeanShell调用jar包对HTTP请求中的参数进行MD5加密

    前提: eclipse.JDK.Jmeter 说明: 本文分为两部分进行配置说明 第一部分:编写JavaMD5加密脚本 第二部分:使用Jmeter的BeanShell进行验证 ************ ...

  5. mysql关联查询和联合查询

    一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...

  6. 支付宝支付-常用支付API详解(查询、退款、提现等)-转

    所有的接口支持沙盒环境的测试 1.前言 前面几篇文件详细介绍了 支付宝提现.扫码支付.条码支付.Wap支付.App支付 支付宝支付-提现到个人支付宝 支付宝支付-扫码支付 支付宝支付-刷卡支付(条码支 ...

  7. 10.2.2移动产品离线功能等具体解释----暨4月8日移动《在离线一体化》公开课Q&amp;A

    4月8日<离,或者不离,ArcGIS移动的"在离线一体化"就在那里!>移动公开课已经结束,针对公开课上粉丝们重点关注的问题,本博客进行了具体的解答.答疑主要环绕最新的R ...

  8. go omitempty 忽略类型

    nil false 0 每个结构字段的编码可以通过结构字段标签中“json”键下存储的格式字符串来定制.格式字符串给出字段的名称,可能后跟逗号分隔的选项列表.名称可能为空,以指定选项而不覆盖默认字段名 ...

  9. 仿LOL项目开发第七天

    仿LOL项目开发第七天 by 草帽 不知不觉已经写到了第七篇这种类型的博客,但是回过头看看之前写的,发现都只能我自己能看懂. 我相信在看的童鞋云里雾里的,因为我基本上没怎么详细讲一个脚本怎么用?但是你 ...

  10. 一个VLAN配置的实际例子

    背景很简单,和一般的eth-switch通过VLAN做成路由的方式一样.     首先看一种硬件效率较高的方法: Port1~4作为access口,同时在硬件上作为用户模式,即从PC发往这些端口的数据 ...