[Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript
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的更多相关文章
- [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript
The median maintenance problem is a common programming challenge presented in software engineering j ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
- LeetCode之“动态规划”:Maximum Product Subarray
题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...
随机推荐
- java中TCP总结
先看一张图,画的很挫,将就看. TCP 客户端与服务端通信时,是服务端会拿到客户端的socket进行通信. TCP就相当于以前的座机,有一个听筒和一个话筒,A用话筒说话,B用听筒听. 下面讲讲java ...
- How to install VIB on VMware ESXi
What is VIB? A vib is vSphere Installation Bundle. To make it more detailed, one can look at the vS ...
- 强制打开qq
(function(){ var QQ='10001'; //换成你公司的企业QQ(客服QQ) var str='tencent://message/?Menu=yes&uin='+QQ+'& ...
- 傻逼数学题(math)
傻逼数学题 题目描述 由于乱码,复制不下来,只好截图了 输入格式 第一行一个正整数n 接下来n行每行两个整数x,y,表示第i个点的坐标 输出格式 一行一个数表示最小距离和,保留6位小数 样例输入 4 ...
- RelativeSource
当一个Binding有明确的数据来源时可以通过为Source或ElementName赋值的办法让Binding与之关联,有的时候由于不能确定Source的对象叫什么名字,但知道它与作为Binding目 ...
- c /c++变参函数(转)
原文转自 https://blog.csdn.net/wwzcx/article/details/8940092 实现c/c++语言的变参函数. 变参函数 :void fun(para,...) 变参 ...
- ldd命令【转】
转自:http://www.cnblogs.com/wanghetao/p/3779611.html ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库. 参数说明: --versi ...
- VS2013 MFC C++ CString ,const char , char, string 类型转换
VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...
- HTML,DIV+CSS,js,JQ,UI-WEB前端设计经验
目前比较全的CSS重设(reset)方法总结 在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览 ...
- itatis中的数据库配置
<!--com.microsoft.sqlserver.jdbc.SQLServerDriver --> <property name="JDBC.Driver" ...