-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path053_MaximumSubarray.cpp
62 lines (60 loc) · 1.34 KB
/
053_MaximumSubarray.cpp
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
//48.13%
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.empty()) return -1;
int size(nums.size());
int maxSum(nums[0]), currSum(nums[0]);
for(int i = 1; i < size; i++){
if(nums[i] < 0){
maxSum = max(maxSum, currSum);
if(currSum + nums[i] < 0){
if(currSum >= 0)
currSum += nums[i];
else
currSum = nums[i];
}else
currSum += nums[i];
}else{
if(currSum < 0)
currSum = nums[i];
else
currSum += nums[i];
}
}
return max(maxSum, currSum);
}
};
//6.88%
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.empty()) return -1;
int size(nums.size());
int maxSum(nums[0]), currSum(nums[0]);
for(int i = 1; i < size; i++){
if(currSum < 0)
currSum = nums[i];
else
currSum += nums[i];
maxSum = max(maxSum, currSum);
}
return max(maxSum, currSum);
}
};
/*
Idea is very simple. Basically, keep adding each integer to the sequence until the sum drops below 0.
If sum is negative, then should reset the sequence.
*/
class Solution {
public:
int maxSubArray(int A[], int n) {
int ans=A[0],i,j,sum=0;
for(i=0;i<n;i++){
sum+=A[i];
ans=max(sum,ans);
sum=max(sum,0);
}
return ans;
}
};