We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e774921 commit d2357c6Copy full SHA for d2357c6
C++/maximum-sum-circular-subarray.cpp
@@ -0,0 +1,19 @@
1
+// Time: O(n)
2
+// Space: O(1)
3
+
4
+class Solution {
5
+public:
6
+ int maxSubarraySumCircular(vector<int>& A) {
7
+ int total = 0;
8
+ int max_sum = numeric_limits<int>::min(), cur_max = 0;
9
+ int min_sum = numeric_limits<int>::max(), cur_min = 0;
10
+ for (const auto& a : A) {
11
+ cur_max = max(cur_max + a, a);
12
+ max_sum = max(max_sum, cur_max);
13
+ cur_min = min(cur_min + a, a);
14
+ min_sum = min(min_sum, cur_min);
15
+ total += a;
16
+ }
17
+ return max_sum > 0 ? max(max_sum, total - min_sum) : max_sum;
18
19
+};
0 commit comments