[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 ...
随机推荐
- 内部类(inner class)的简单介绍
本文主要介绍内部类(inner class)的一些基本应用,将从内部类的分类角度,首先对每一个具体内部类进行介绍.主要包括普通的内部类[common inner class].局部内部类[local ...
- 图表绘制工具--Matplotlib 2
''' [课程3.] 基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式 plt.plot(kind='line', ax=No ...
- java:网络编程(InetAddress,InetSocketAddress,URL,TCP(Socket与SeverSocket),TCP与UDP的区别)
InerAddress: /**IP地址:在网络上唯一标示一台计算机 * 端口号:标示计算机上不同的应用程序 * java.net.InetAddress类:此类表示互联网协议 (IP) 地址. * ...
- POJ1200 Crazy Search
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Many peo ...
- JSP、JSTL、EF学习笔记
JSP 1)Java Server Page,在html中嵌入java代码 2)九个内置(隐式)对象 request response out page pageContext config sess ...
- Hibernate中的延迟加载及fetch
Hibernate中的延迟加载 1.类级别的查询策略: lazy : true(默认值) false(立即加载) 2.多对一关联的查询策略: lazy: proxy(默认值) no-proxy ...
- 调试钩取技术 - 记事本WriteFile() API钩取
@author: dlive 0x01 简介 本章将讲解前面介绍过的调试钩取技术,钩取记事本的kernel32!WriteFile() API 调试钩取技术能进行与用户更具有交互性(interacti ...
- 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本
;~ 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本 ;~ 作者: 鹏程万里 ;~ Email:aprial@163.com ;~ 创建日期: 2014年11 ...
- 判断dataset是否被修改—DataSet.HasChanges 方法
DataSet.HasChanges 方法 获取一个值,该值指示 DataSet 是否有更改,包括新增行.已删除的行或已修改的行. 命名空间: System.Data程序集: System.Da ...
- jmeter 多机负载压测与服务器性能监测
环境: jmeter: apache-jmeter-3.3 jdk: 负载生成: 被测机: 一. 多机负载压测: 概述: 1.修改jmeter.properties配置文件 remote_hosts= ...