-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumSubarray.js
More file actions
47 lines (40 loc) · 1.5 KB
/
maximumSubarray.js
File metadata and controls
47 lines (40 loc) · 1.5 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
46
47
/**
* LeetCode 53. Maximum Subarray
* https://leetcode.com/problems/maximum-subarray/description/
*
* Given an integer array nums, find the subarray with the largest sum,
* and return its sum.
*/
/**
* @param {number[]} nums
* @return {number}
*/
function maxSubArray(numbers) {
// If the array is empty, the maximum sum is 0.
if (numbers.length === 0) {
return 0;
}
// We initialize both trackers with the first element of the array.
// 'currentSubarraySum' tracks the best sum ending at the current position.
// 'highestSumFoundSoFar' tracks the global maximum across the entire array.
let currentSubarraySum = numbers[0];
let highestSumFoundSoFar = numbers[0];
// We start iterating from the second element (index 1).
for (let i = 1; i < numbers.length; i++) {
const currentNumber = numbers[i];
/**
* DECISION POINT:
* We compare the current number itself against (current number + previous subarray sum).
* * If the current number is greater than the combined sum, it means the
* previous subarray was "dragging us down" (negative), so we discard
* it and start a fresh subarray at the current number.
*/
currentSubarraySum = Math.max(currentNumber, currentSubarraySum + currentNumber);
// After updating the current subarray, we check if it's the best we've ever seen.
if (currentSubarraySum > highestSumFoundSoFar) {
highestSumFoundSoFar = currentSubarraySum;
}
}
return highestSumFoundSoFar;
}
module.exports = { maxSubArray };