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 =  to length
memo[i] = Math.max(memo[i - ], memo[i - ] + arr[i])

Code:

function maxNonAdjSum(arr) {
const memo = new Array(arr.length).fill();
memo[] = Math.max(, arr[]);
memo[] = Math.max(arr[], memo[]); for (let i = ; i < arr.length; i++) {
memo[i] = Math.max(memo[i-], arr[i] + memo[i - ]);
} return memo;
} console.log(maxNonAdjSum([, , , , ])); //
console.log(maxNonAdjSum([, , , ])); // 10
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:

// [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(, arr[]);
let max_not_inc = Math.max(arr[], max_inc); let max = max_inc
for (let i = ; 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([, , , , ])); //
console.log(maxNonAdjSum2([, , , ])); //
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. [POI2015]Pustynia

    [POI2015]Pustynia 题目大意: 给定一个长度为\(n(n\le10^5)\)的正整数序列\(a\),每个数都在\(1\)到\(10^9\)范围内,告诉你其中\(s\)个数,并给出\(m ...

  2. lnmp环境一键搭建及卸载

    系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...

  3. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  4. C#高级编程9-第3章 对象与类型

    类与结构 类和结构都是对象的模板 类定义了处理和访问数据的方法,通过类的实例化进行逻辑处理 类与结构的区别是类是引用类型,存储在托管堆上:结构是值类型,存储在栈上的: 类使用class进行修饰,结构使 ...

  5. 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”

    报错内容如下: 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象强制转换为接口类型“Microsoft.Office.Inte ...

  6. java中write(byte[] b)与write(byte[] b,int off,int len)区别

    public static void copyInputStreamT0OutputStream(InputStream in, OutputStream out) { byte[] buffer = ...

  7. SVN服务端的版本对比及创建仓库时的注意事项

    SVN是一个开放源代码的版本控制系统,分为客户端和服务端.就windows系统而言,客户端通常使用 TortoiseSVN,下载地址:https://tortoisesvn.net/  ,而服务端通常 ...

  8. DHT(Distributed Hash Table,分布式哈希表)

    DHT(Distributed Hash Table,分布式哈希表)类似Tracker的根据种子特征码返回种子信息的网络. DHT全称叫分布式哈希表(Distributed Hash Table),是 ...

  9. DotNet_Performance_Tuning_ANTS_Performance_Profiler

    http://www.cnblogs.com/parry/archive/2013/01/04/DotNet_Performance_Tuning_ANTS_Performance_Profiler. ...

  10. jsp下Kindeditor环境搭建

    1.环境:tomcat 2.需要外部jar包: commons-fileupload-1.2.1.jar commons-io-1.4.jar json_simple-1.1.jar 3.下载Kind ...