-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53.maximum-subarray.cpp
More file actions
129 lines (110 loc) · 2.79 KB
/
53.maximum-subarray.cpp
File metadata and controls
129 lines (110 loc) · 2.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem53.h"
using namespace std;
// @before-stub-for-debug-end
/*
* @lc app=leetcode id=53 lang=cpp
*
* [53] Maximum Subarray
*
* https://leetcode.com/problems/maximum-subarray/description/
*
* algorithms
* Easy (46.11%)
* Likes: 7786
* Dislikes: 363
* Total Accepted: 1M
* Total Submissions: 2.2M
* Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]'
*
* Given an integer array nums, find the contiguous subarray (containing at
* least one number) which has the largest sum and return its sum.
*
* Example:
*
*
* Input: [-2,1,-3,4,-1,2,1,-5,4],
* Output: 6
* Explanation: [4,-1,2,1] has the largest sum = 6.
*
*
* Follow up:
*
* If you have figured out the O(n) solution, try coding another solution using
* the divide and conquer approach, which is more subtle.
*
*/
/** Solution
* Select the p-th number of nums(0 < p < nums.size() - 1)
* as the pivot. The maximum subarray[l,r] must
* lies in one of the following situations:
* (1). left to pivot (l <= r <= p)
* (2). rigt to pivot (p+1 <= l <=r)
* (3). cross the pivot (l < p < r)
*
* for (1) and (2) we can call maximum subarray method
* recursively.
*
* for (3), we need to find maximum subarray[i, p] and
* maximum subarray [p+1,j]
*
* Finally, return max((1), (2), (3))
*/
#include <vector>
using std::vector;
// @lc code=start
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int r = nums.size() - 1, l = 0;
return maxSubArrayHelper(nums, l, r);
}
int maxSubArrayHelper(vector<int>& nums, const int l, const int r) {
if (l == r)
{
return nums[l];
}
int p = (l + r) / 2;
int lmax = maxSubArrayHelper(nums, l, p);
int rmax = maxSubArrayHelper(nums, p+1, r);
int cmax = crossingMaxSubArray(nums, l, r, p);
if (lmax < rmax)
{
lmax = rmax;
}
if (lmax < cmax)
{
lmax = cmax;
}
return lmax;
}
int crossingMaxSubArray(vector<int>& nums, const int l, const int r, const int p) {
int leftSum = INT_MIN, sum = 0;
//int left;
for (int i = p; i >= l; i--)
{
sum += nums[i];
if (sum > leftSum)
{
leftSum = sum;
//left = i;
}
}
int rightSum = INT_MIN;
sum = 0;
//int right;
for (int i = p + 1; i <= r; i++)
{
sum += nums[i];
if (sum > rightSum)
{
rightSum = sum;
//right = i;
}
}
return (leftSum + rightSum);
}
};
// @lc code=end