diff --git a/11 November Leetcode Challenge 2021/25_maxSubArray.cpp b/11 November Leetcode Challenge 2021/25_maxSubArray.cpp new file mode 100644 index 0000000..e10ea88 --- /dev/null +++ b/11 November Leetcode Challenge 2021/25_maxSubArray.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector pre, suf; + int maxSubArray(vector& nums) { + pre = suf = nums; + for(int i = 1; i < size(nums); i++) pre[i] += max(0, pre[i-1]); + for(int i = size(nums)-2; ~i; i--) suf[i] += max(0, suf[i+1]); + return maxSubArray(nums, 0, size(nums)-1); + } + int maxSubArray(vector& A, int L, int R){ + if(L == R) return A[L]; + int mid = (L + R) / 2; + return max({ maxSubArray(A, L, mid), maxSubArray(A, mid+1, R), pre[mid] + suf[mid+1] }); + } +};