[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 return13
, since we pick2
,6
, and5
.[5, 1, 1, 5]
should return10
, since we pick5
and5
.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 ...
随机推荐
- android view surfaceView GLSurfaceView
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 表面视图 SurfaceView 是 视图 的子类, 刷新界面速度比 视图 块, 因为它 ...
- TCP的三次握手与四次释放
TCP的三次握手与四次释放 一.名词解释 序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上序号后,就给 ...
- loj#2721. 「NOI2018」屠龙勇士
题目链接 loj#2721. 「NOI2018」屠龙勇士 题解 首先可以列出线性方程组 方程组转化为在模p意义下的同余方程 因为不保证pp 互素,考虑扩展中国剩余定理合并 方程组是带系数的,我们要做的 ...
- POJ 2117 Electricity 双联通分量 割点
http://poj.org/problem?id=2117 这个妹妹我竟然到现在才见过,我真是太菜了~~~ 求去掉一个点后图中最多有多少个连通块.(原图可以本身就有多个连通块) 首先设点i去掉后它的 ...
- 【推导】Codeforces Round #484 (Div. 2) C. Cut 'em all!
题意:给你一棵树,让你切掉尽可能多的边,使得产生的所有连通块都有偶数个结点. 对于一棵子树,如果它有奇数个结点,你再从里面怎么抠掉偶数结点的连通块,它都不会变得合法.如果它本来就有偶数个结点,那么你怎 ...
- Lunix/Mac下根据最后修改时间复制文件和文件夹,保持原有的目录结构
度娘知道:http://zhidao.baidu.com/link?url=DD47jm6qDgT7yxsnz9e-NC4Fqd33oRoiIwcGLkw5TL4cbf50VKY2IONbHKH0IE ...
- euclidea 3.0 全三星 攻略
euclidea攻略 游戏地址 http://www.euclidea.xyz/en/game/#/packs 攻略 Alpha level : 1.1 line tool 3L3E 智障题 1.2 ...
- git服务器安装和配置
http://www.cnblogs.com/dee0912/p/5815267.html
- Git_添加远程库
现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举 ...
- 使用静态库的一些问题 -all_load
1.使用类目在我们的静态库中涉及到 类目 catagory的使用时,会崩溃:此时我们需要设置project的Info里面的Link Flag处,增加-all_load,这样会链接所以存在的symbol ...