[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 ...
随机推荐
- hdu 4544 优先队列+贪心
题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...
- webwork或Struts配置网站根路径的默认页面办法
参考资料:http://www.iteye.com/problems/24028 查阅好多资料,关于webwork或Struts处理默认页面的方式,能否像spring MVC那样直接指定默认访问页面. ...
- python数据库操作——sqlite3模块
# -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...
- jmeter用BeanShell调用jar包对HTTP请求中的参数进行MD5加密
前提: eclipse.JDK.Jmeter 说明: 本文分为两部分进行配置说明 第一部分:编写JavaMD5加密脚本 第二部分:使用Jmeter的BeanShell进行验证 ************ ...
- mysql关联查询和联合查询
一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...
- 支付宝支付-常用支付API详解(查询、退款、提现等)-转
所有的接口支持沙盒环境的测试 1.前言 前面几篇文件详细介绍了 支付宝提现.扫码支付.条码支付.Wap支付.App支付 支付宝支付-提现到个人支付宝 支付宝支付-扫码支付 支付宝支付-刷卡支付(条码支 ...
- 10.2.2移动产品离线功能等具体解释----暨4月8日移动《在离线一体化》公开课Q&A
4月8日<离,或者不离,ArcGIS移动的"在离线一体化"就在那里!>移动公开课已经结束,针对公开课上粉丝们重点关注的问题,本博客进行了具体的解答.答疑主要环绕最新的R ...
- go omitempty 忽略类型
nil false 0 每个结构字段的编码可以通过结构字段标签中“json”键下存储的格式字符串来定制.格式字符串给出字段的名称,可能后跟逗号分隔的选项列表.名称可能为空,以指定选项而不覆盖默认字段名 ...
- 仿LOL项目开发第七天
仿LOL项目开发第七天 by 草帽 不知不觉已经写到了第七篇这种类型的博客,但是回过头看看之前写的,发现都只能我自己能看懂. 我相信在看的童鞋云里雾里的,因为我基本上没怎么详细讲一个脚本怎么用?但是你 ...
- 一个VLAN配置的实际例子
背景很简单,和一般的eth-switch通过VLAN做成路由的方式一样. 首先看一种硬件效率较高的方法: Port1~4作为access口,同时在硬件上作为用户模式,即从PC发往这些端口的数据 ...