[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 ...
随机推荐
- Git的优点
没有网络时也可以使用版本控制系统,这点svn做不到,如果你一直有网络,这个可以忽略: git由于所有版本都在本地的.git目录数据库中,因此它可以用指针随时改变指向,指向不同的版本,把它作为最新的he ...
- [1]区分event对象中的[clientX,offsetX,screenX,pageX]
前言 在平时的开发中,非常讨厌的就是兼容性了,兼容性的问题总会让我们记忆混淆,所以这次来区分一下event对象中的常用获取鼠标位置. clientX clientY event.clientXeven ...
- [02]a tag只为成button用时候设置href的办法
a tag为成button使用,把JavaScript动作处理时,有如下四种停止Event效果. <a href="#"> <a href="javas ...
- 【转】shell脚本写的俄罗斯方块游戏
亲测一个很好玩的shell脚本写的俄罗斯方块游戏,脚本来自互联网 先来讲一下思维流程 一.方块的表示 由于shell不能定义二维数组,所以只能用一维数组表示方块,俄罗斯方块主要可以分为7类,每一类方块 ...
- Ubuntu系统用户与用户组
1.查看用户组 vi /etc/group 结果说明: 组名: 组名是用户组的名称,由字母或数字构成.与/etc/passwd中的登录名一样,组名不应重复. 口令: 口令字段存放的是用户组加密后的 ...
- 各版本Sql Server下载地址全
SQL Server 2014简体中文企业版 文件名:cn_sql_server_2014_enterprise_edition 32位下载地址:ed2k://|file|cn_sql_server_ ...
- win8启用.net framework3.5方法
管理员命令下执行dism.exe /online /enable-feature /featurename:NetFX3 /Source:L:\sources\sxs
- Strlcpy和strlcat——一致的、安全的字符串拷贝和串接函数【转】
转自:http://blog.csdn.net/kailan818/article/details/6731772 英文原文: http://www.gratisoft.us/todd/papers/ ...
- python3使用urllib获取set-cookies
#!/usr/bin/env python # encoding: utf-8 import urllib.request from collections import defaultdict re ...
- java1.7集合源码阅读:ArrayList
ArrayList是jdk1.2开始新增的List实现,首先看看类定义: public class ArrayList<E> extends AbstractList<E> i ...