【範例】環狀陣列之最大連續和
【題目敘述】https://leetcode.com/problems/maximum-sum-circular-subarray/
class Solution {
public:
int maxSubarraySumCircular(vector<int>& A) {
int mx = A[0], alltot = 0, tot1 = 0, tot2, mxtot = 0, mntot = 0;
for (int i = 0; i < A.size(); i++){
mx = max(mx, A[i]);
alltot += A[i];
tot1 += A[i];
mxtot = max(mxtot, tot1);
tot2 += A[i];
mntot = min(mntot, tot2);
if (tot1 < 0) tot1 = 0;
if (tot2 > 0) tot2 = 0;
}
mxtot = max(mxtot, alltot - mntot);
if (mx < 0) return mx;
else return mxtot;
}
};