forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0918.cpp
More file actions
executable file
·42 lines (38 loc) · 913 Bytes
/
LC0918.cpp
File metadata and controls
executable file
·42 lines (38 loc) · 913 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Problem Statement: https://leetcode.com/problems/maximum-sum-circular-subarray/
Time: O(n)
Space: O(1)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
int maxSubarraySumCircular(vector<int>& A) {
int max_sum, min_sum, sum, n;
n = A.size();
max_sum = maximum_subarray(A);
min_sum = minimum_subarray(A);
sum = accumulate(A.begin(), A.end(), 0);
if (sum == min_sum)
return max_sum;
else
return max(sum - min_sum, max_sum);
}
int maximum_subarray(vector<int>& A) {
int max_sum, sum;
max_sum = sum = A[0];
for (int i = 1; i < A.size(); i++) {
sum = max(A[i], sum + A[i]);
max_sum = max(sum, max_sum);
}
return max_sum;
}
int minimum_subarray(vector<int>& A) {
int min_sum, sum;
min_sum = sum = A[0];
for (int i = 1; i < A.size(); i++) {
sum = min(A[i], sum + A[i]);
min_sum = min(sum, min_sum);
}
return min_sum;
}
};