forked from paopao2/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum Subarray.js
71 lines (55 loc) · 1.54 KB
/
Maximum Subarray.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
var sum = 0,
max = Number.NEGATIVE_INFINITY,
len = nums.length,
i;
for (i = 0; i < len; i++) {
sum += nums[i];
max = Math.max(sum, max);
if (sum < 0) {
sum = 0;
}
}
return max;
};
// divide and conquer
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
return helper(0, nums.length - 1, nums);
};
function helper(start, end, arr) {
if (start > end) {
return Number.NEGATIVE_INFINITY;
}
if (start === end) {
return arr[start];
}
var mid = Math.floor((start + end) / 2),
leftMax = Number.NEGATIVE_INFINITY,
rightMax = Number.NEGATIVE_INFINITY,
midMax,
i,
curSum;
for (i = mid - 1, curSum = 0; i >= start; i--) {
curSum += arr[i];
leftMax = Math.max(curSum, leftMax);
}
for (i = mid + 1, curSum = 0; i <= end; i++) {
curSum += arr[i];
rightMax = Math.max(curSum, rightMax);
}
midMax = arr[mid] + Math.max(leftMax, 0) + Math.max(rightMax, 0);
return Math.max(midMax, helper(start, mid - 1, arr), helper(mid + 1, end, arr));
}