[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 ...
随机推荐
- C# 集合类-接口
所谓,程序=数据结构+算法. 我目前的日常工作就是繁琐的业务流程和增删改查之类的. 其实繁琐的业务流程也不过是改变一下数据的状态.怪不得叫,面向数据库编程.哈哈. 所以呢,了解一下各种 .net内置的 ...
- bzoj3456 城市规划 多项式求In
\(n\)个点的无向联通图的个数 打着好累啊 一定要封装一个板子 记\(C(x)\)为无向图个数的指数型生成函数,\(C(0) = 1\) 记\(G(x)\)为无向联通图个数的指数型生成函数,\(G( ...
- hihocoder 1866 XOR
题面在这里 拆位分析一下就OK啦 /* y + (y xor x) */ #include<bits/stdc++.h> #define ll long long using namesp ...
- [HDU6198]number number number
题目大意: 给定一个数k表示你可以从包括0的斐波那契数列中任取k个数,得到它们的和.求最小的不能得到的自然数. 思路: 打表找规律,可以发现答案为f(2k+3)-1,然后用公式f(i)=f(i/2)* ...
- Go Web编程 第三章--接收请求
net/http标准库 net/http标准库通常包括两个部分,客户端和服务器,我们可以通过ListenAndServe创建一个简陋的服务器 package main import ( "n ...
- 【8.14校内测试】【DP专题】
nlogn做法,dp[i]表示当前长度为i的最长上升子序列末尾元素的值. 不会写lower_bound(qwq,贴一个以前的好看点的代码 #include<iostream>//使用low ...
- Mysql的学习随笔day1
关于mysql的基本语句 ps:[]是缺省 创建:CREATE DATABASE db.name CREATE TABLE name(列名,类型,[NULL])NOT NULL是不需要为空,NOT ...
- python开发_thread_线程_搜索本地文件
在之前的blog中,曾经写到过关于搜索本地文件的技术文章 如: java开发_快速搜索本地文件_小应用程序 python开发_搜索本地文件信息写入文件 下面说说python中关于线程来搜索本地文件 利 ...
- Jmeter实现对mysql的增、删、改、查
1. 创建一个存储过程,语句如下: DELIMITER $$; DROP PROCEDURE IF EXISTS test; create PROCEDURE test() BEGI ...
- SecureCRT 如何改变对话框--每行输入字符数的多少
通过设置Logical columns的大小,选中Retain size an font来改变每行输入字符数的多少 1.设置Session Options–>Terminal–>Emula ...