[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
0or negative.For example,
[2, 4, 6, 2, 5]should return13, since we pick2,6, and5.[5, 1, 1, 5]should return10, since we pick5and5.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的更多相关文章
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- 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 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- [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 ...
随机推荐
- 友好的KVO
更友好的KVO 前言 观察者模式是大家在开发过程中每个人都要使用的一种设计模式,在iOS的开发流程中,KVO则是这一开发模式的主要实践手段,观察一个属性,当属性值发生变化的就能能够拿到这个属性的新.老 ...
- [POJ2337]Catenyms
题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...
- 开源中国上抓取的content-type
开源中国上抓取的content-type类型,来源:http://www.cnblogs.com/smallyard/p/5632608.html { ".*": "ap ...
- python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名
import MySQLdb #connect try: conn = MySQLdb.connect( host = "localhost", user = "root ...
- Java中日期类型和mysql中日期类型进行整合
1. java与mysql中日期.时间类型总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mysql(版本:5.1.50)的时间日期类型如下: da ...
- ZOJ 2969 Easy Task
E - Easy Task Description Calculating the derivation of a polynomial is an easy task. Given a functi ...
- 理解 JavaScript 中的 Function.prototype.bind
函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其 ...
- 移动端适配之REM
随着手机等移动设备的普及,移动端带来的流量已经不可忽视,一个网站不只是只有pc的页面就足够了,移动端的适配已经势在必行.但是移动设备种类繁多,屏幕尺寸也千奇百怪,能不能找到一种方式可以适配所有的手机屏 ...
- SQL 死锁进程查询
use master go declare @spid int,@bl int DECLARE s_cur CURSOR FOR ,blocked ) a ) b where a.blocked=sp ...
- DeveloperAppleHelp
UIKit: 1.UIKit User Interface Catalog 视图 View控件 2.View Programming Guide for iOS 视图编程,用代码 构建界面. 3. ...