1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public: struct Status { int lSum, rSum, mSum, iSum; };
Status pushUp(Status l, Status r) { int iSum = l.iSum + r.iSum; int lSum = max(l.lSum, l.iSum + r.lSum); int rSum = max(r.rSum, r.iSum + l.rSum); int mSum = max({l.mSum, r.mSum, l.rSum + r.lSum}); return (Status) {lSum, rSum, mSum, iSum}; };
Status get(vector<int> &a, int l, int r) { if (l == r) { return (Status) {a[l], a[l], a[l], a[l]}; } int m = l + r >> 1; Status lSub = get(a, l, m); Status rSub = get(a, m + 1, r); return pushUp(lSub, rSub); }
int maxSubArray(vector<int>& nums) { return get(nums, 0, nums.size() - 1).mSum; } };
|