Created
November 3, 2018 03:15
-
-
Save fpdjsns/fa784f6bf9e9296b8670fc07759ff466 to your computer and use it in GitHub Desktop.
[leetcode] 918. Maximum Sum Circular Subarray : https://leetcode.com/problems/maximum-sum-circular-subarray/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxSubarraySumCircular(vector<int>& A) { | |
int ans = A[0]; | |
int maxSub = A[0]; | |
int minSub = A[0]; | |
int sum = 0; | |
int sumMax = 0; | |
int sumMin = 0; | |
int maxNum = A[0]; | |
for(int i=0; i< A.size(); i++){ | |
sumMax = sumMax + A[i] > 0 ? sumMax + A[i] : 0; | |
maxSub = max(maxSub, sumMax); | |
sumMin = sumMin + A[i] < 0 ? sumMin + A[i] : 0; | |
minSub = min(minSub, sumMin); | |
sum += A[i]; | |
maxNum = max(maxNum, A[i]); | |
} | |
return maxNum < 0 ? maxNum : max(maxSub, sum - minSub); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment