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. AE中实现Control中的各种图形工具的方法

    添加命名空间 using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.Controls; A类:前面有Controls 后面有tool的工具都可以用同一类的代码实现( ...

  2. 一种有效的压缩感知方法——读Levin论文笔记

    原文链接:http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.122.2942&rep=rep1&type=pdf 1 基 ...

  3. unicode 编码在线转换工具--javascript

    http://www.cnblogs.com/mq0036/p/4007452.html

  4. Springboot-plus 安装使用的一些问题

    最近在研究一些springboot的框架,然后看到了这个款 springboot  plus ,下载下来研究下. 将安装部署的一些问题记录下来了. 第一个,就是部署的时候,我使用的MySQL数据库,导 ...

  5. 关于js的addEventListener 和一些常用事件

    element.addEventListener(<event-name>, <callback>, <use-capture>);document.addEven ...

  6. foj Problem 2282 Wand

     Problem 2282 Wand Accept: 432    Submit: 1537Time Limit: 1000 mSec    Memory Limit : 262144 KB Prob ...

  7. struts2中的文件上传和文件下载

    单文件文件上传 1.

  8. HDU1083_Courses_课程_C++

    给你们人工翻译一下题目哈,刚好练一下英语 对于一个组中有 N 个学生和 P 种课程.每个学生能够参加0种,1种或者更多的课程.你的任务是找到一种可能的方案使恰好P个学生同时满足下列条件: ‧ 方案中的 ...

  9. 使用 WideCharToMultiByte Unicode 与 UTF-8互转

    1.简述 最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们 ...

  10. urllib url解析学习

    #!/usr/bin/env python # encoding: utf-8 from urllib.parse import * #urlparse:解析url分段 #urlsplit:类似url ...