spider68

IMP : MAXIMUM SUM OF circular subarray

May 15th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. int maxSubarraySumCircular(vector<int>& A) {
  2.         int total = 0, maxSum = -30000, curMax = 0, minSum = 30000, curMin = 0;
  3.         for (int a : A) {
  4.             curMax = max(curMax + a, a);
  5.             maxSum = max(maxSum, curMax);
  6.             curMin = min(curMin + a, a);
  7.             minSum = min(minSum, curMin);
  8.             total += a;
  9.         }
  10.         return maxSum > 0 ? max(maxSum, total - minSum) : maxSum;
  11.     }
Add Comment
Please, Sign In to add comment