-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumSubarray.js
More file actions
45 lines (42 loc) · 1.22 KB
/
maximumSubarray.js
File metadata and controls
45 lines (42 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @param {number[]} nums
* @return {number}
*
* https://leetcode.com/problems/maximum-subarray/submissions/
*/
/**
* O(n^2) solution
* Iterate through the array finding the sum of all possible subwindows and memoize maxSum
* (since the sum of the subwindow between nums(i, j) is (i, j-1) + nums[j]
* we do not have to actually iterate over all possible subwindows which is On^3)
*/
var maxSubArray = function(nums) {
let maxSum = nums[0]
let currSum = 0
for (let i = 0; i < nums.length; i++) {
for (let j = i; j < nums.length; j++) {
currSum += nums[j]
if (currSum > maxSum) {
maxSum = currSum
}
}
currSum = 0
}
return maxSum
};
/**
* Linear time solution
*
* Here we are more efficient by knowing that the maxSum for an array from (1, j),
* is either the max sum of the sub array (1, j-1) or the maxSum of a contiguous subarray that contains
* the current index j.
*/
var maxSubArray = function(nums) {
let maxSum = nums[0]
let maxSumIncludingCurrIndex = nums[0]
for (let i = 1; i < nums.length; i++) {
maxSumIncludingCurrIndex = Math.max(nums[i], nums[i] + maxSumIncludingCurrIndex)
maxSum = Math.max(maxSum, maxSumIncludingCurrIndex)
}
return maxSum
};