-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSubarray.js
More file actions
26 lines (21 loc) · 713 Bytes
/
maxSubarray.js
File metadata and controls
26 lines (21 loc) · 713 Bytes
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
/**
* 53. Maximum Subarray
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
*/
let nums = [-2,1,-3,4,-1,2,1,-5,4]
function maxSubArray(nums){
let currentSum = nums[0] // current running sum
let maxSum = nums[0] // max sum so far
for(let i=1; i<nums.length; i++){
// Either extend the current subarray or start a new one
currentSum = Math.max(nums[i], currentSum+nums[i])
// Update global maximum if needed
maxSum = Math.max(currentSum, maxSum)
}
return maxSum
}
console.log(maxSubArray(nums))