Naive solution for this problem would be caluclate all the possible combinations:

const numbers = [1, -3, 2 - 5, 7, 6, -1, -4, 11, -23];

// O(n^3)
const findMaxSubAry = numbers => {
let answer = Number.MIN_VALUE;
/**
* Calculate all the possible values and pick the max one
* All possible values should be
* length = 1, 2, ,3 ... n
* Pick differnet start point
*/ // For different lenght
for (let l = 0; l < numbers.length; l++) {
// O(n)
// For different start
for (let s = 0; s < l; s++) {
// O(n)
if (s + l >= numbers.length) {
break;
}
let sum = 0;
for (let i = s; i < s + l; i++) {
// O(n)
sum += numbers[i];
} answer = Math.max(answer, sum);
}
} return answer;
}; console.log(findMaxSubAry(numbers)); //

The maximum subarray problem is one of the nicest examples of dynamic programming application.

In this lesson we cover an example of how this problem might be presented and what your chain of thought should be to tackle this problem efficiently.

 /**
* Maximum Contiguous subarray algorithm
*
* Max(i) = Max(i-1) + v(i)
* Max(i-1) < 0 ? v(i) : Max(i-1)
*
* Combining
---------
maxInc(i) = maxInc(i - 1) > 0 ? maxInc(i - 1) + val(i) : val(i)
max(i) = maxInc(i) > max(i - 1) ? maxInc(i) : max(i - 1)
*/
function maxSumSubArray(arr) {
/**
* inx | val | max_inc | max
* 0 0 0
* 0 -2 0 0
* 1 -3 0 0
* 2 4 4 4 ---> start = 2
* 3 -1 3 4
* 4 -2 1 4
* 5 1 2 4
* 6 5 7 7 ---> end = 6
* 7 -3 4 7
*/ let val = , max_inc = , max = , start = , end = ; for (let i = ; i < arr.length; i++) {
val = arr[i];
max_inc = Math.max(max_inc + val, val);
max = Math.max(max, max_inc); if (val === max_inc) {
start = i;
} if (max === max_inc) {
end = i;
}
} if (end === ) {
end = start;
}
console.log(start, end);
return arr.slice(start, end + );
} console.log(maxSumSubArray([-, -, , -, -, , , -])); //[4, -1, -2, 1, 5]
console.log(maxSumSubArray([-,-,-,-,-])); // [-2]

[Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript的更多相关文章

  1. [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript

    The median maintenance problem is a common programming challenge presented in software engineering j ...

  2. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  4. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  5. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  8. 643. Maximum Average Subarray

    Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...

  9. LeetCode之“动态规划”:Maximum Product Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

随机推荐

  1. ubuntu安装出现"删除initramfs-tools时出错",subprocess installed post-installation script returned error exit status 1

    昨日准备重装ubuntu,增大了系统容量,因为前面用到boot分区不到100M,于是这里分区如下 /boot 100M / 30G /home 50G 然后安装快结束时就出现如下图问题 开始以为是镜像 ...

  2. Three Garlands~Educational Codeforces Round 35

    C. Three Garlands time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. hihocoder 1457 后缀自动机四·重复旋律7 求不同子串的和

    描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的是小Hi发现了一部名字叫<十进制进行曲大全>的作品集,顾名思义,这部作品集里有许多作品 ...

  4. mysql 查询结果创建表

    用 SELECT 的结果创建表 关系数据库的一个重要概念是,任何数据都表示为行和列组成的表,而每条 SELECT 语句的结果也都是一个行和列组成的表.在许多情况下,来自 SELECT 的“表”仅是一个 ...

  5. 强联通分量(tarjan算法+算法简介)

    题目描述 ›对于一个有向图顶点的子集S,如果在S内任取两个顶点u和v,都能找到一条从u到v的路径,那么就称S是强连通的.如果在强连通的顶点集合S中加入其他任意顶点集合后,它都不再是强连通的,那么就称S ...

  6. OpenGL函数思考-glColor

    http://blog.csdn.net/shuaihj/article/details/7231980 OpenGL函数思考-glColor 函数原型:      glColor3b,glColor ...

  7. screenshoter 連續截圖工具

    https://pcrookie.com/?p=993 顯示 mouse 設定 Settings -> Saving -> Display mouse cursor

  8. request_mem_region 与 ioremap【转】

    转自:http://blog.csdn.net/alada007/article/details/7700125 如果从根本上说起的话应该从Intel的处理器芯片与其它的芯片的不同说起,与这两个函数相 ...

  9. js常用函数总结

    字符串函数 indexOf 返回字符串中一个子串第一处出现的索引(从左到右搜索).如果没有匹配项,返回 -1 . var index1 = a.indexOf("l"); //in ...

  10. Git-添加或删除文件

    添加文件 $  git add blash $  git commit "add blash file" $ git push -u origin master 删除文件 $ gi ...