[Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

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)
  */

const numbers = [-2, 1, 3, 4, -1, 2, 1, -5, 4];
function maxSubArray (ary) {
    if (ary.length === 0) {
        return [];
    }

    let maxInc = ary[0];
    let max = ary[0];
    let maxStartInx = 0;
    let maxEndInx = 0;

    for (let i = 0; i < ary.length; i++) {
        const val = ary[i];

        maxInc = Math.max(maxInc + val, val);
        max = Math.max(max, maxInc);

        if (val === max) {
            maxStartInx = i
        }

        if (maxInc === max) {
            maxEndInx = i
        }

        return Array.slice(maxStartInx, maxEndInx + 1)
    }
}
console.log(‘maxSubArray‘, maxSubArray(numbers));

原文地址:https://www.cnblogs.com/Answer1215/p/10227039.html

时间: 2024-11-13 06:23:16

[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 job interviews. 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 probl

Get the largest sum of contiguous subarray in an int array

When I finished reading this problem,I thought I could solve it by scan every single subarray in the array,and the time complexity is cubic.Every subarray could be the eventual one whose sum is the largest,so I did make a conclusion that the best tim

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 

152. Maximum Product Subarray (Array; DP)

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 思路:类似Maximum Subarray,不同的是 max(max_local*n

【LeetCode】152. Maximum Product Subarray

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

Maximum Product Subarray Leetcode

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 这道题和Maximum Sum Subarray类似,也是考虑什么时候可以取得最大值

LeetCode:Maximum Product Subarray

题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题属于动态规划的题型,

LeetCode Maximum Average Subarray I

原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average v