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. 数据准备<4>:变量筛选-理论篇

    在上一篇文章<数据准备<3>:数据预处理>中,我们提到降维主要包括两种方式:基于特征选择的降维和基于维度转换的降维,其中基于特征选择的降维通俗的讲就是特征筛选或者变量筛选,是指 ...

  2. 关于void main()的误区

    很多人甚至市面上的一些书籍,都使用了void main( ) ,其实这是错误的.C/C++ 中从来没有定义过void main( ) .C++ 之父 Bjarne Stroustrup 在他的主页上的 ...

  3. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  4. Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. SVN 提交回滚

    取消对代码的修改分为两种情况:   第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [ ...

  6. [Linux] Ubuntu 18 LTS netplan 网络配置

    Ubuntu 18 LTS netplan 网络配置 今天装完 Ubuntu 18 LTS,配置网络时发现Ubuntu 18LTS ifupdown has been replaced by netp ...

  7. ibatis实战之中的一个对多关联

    在实际开发中,我们经常遇到关联数据的情况,如User对象拥有若干Book对象 每一个Book对象描写叙述了归属于一个User信息,这样的情况下,我们应该怎样处理? 通过单独的Statement操作固然 ...

  8. RPM 打包技术与典型 SPEC 文件分析

    一 .rpm 介绍 1. 概述 RPM全称是 Red Hat Package Manager(Red Hat包管理器).几乎所有的 Linux 发行版本都使用这种形式的软件包管理安装.更新和卸载软件. ...

  9. Python基础教程学习(三)

    如何定义类 class ClassName(base_class[es]): "optional documentation string" static_member_decla ...

  10. linux centos 系统php支持jpeg的安装方法

    linux php支持jpeg首先要安裝libjpeg,运行下面的命令: yum install libjpeg* libpng* freetype* gd* 耐心等待完成,重启(service ht ...