Last active
April 3, 2020 16:16
-
-
Save ObserverHerb/ac34201a82db71b421289c83319f5107 to your computer and use it in GitHub Desktop.
LeetCode - #53 Maximum Subarray: Failed
This file contains hidden or 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
#include <vector> | |
#include <numeric> | |
class Solution { | |
public: | |
int summize(std::vector<int> nums) | |
{ | |
int result=std::accumulate(nums.begin(),nums.end(),0); | |
if (nums.size() == 2) | |
{ | |
if (nums[0] > result || nums[1] > result) return nums[0] > nums[1] ? nums[0] : nums[1]; | |
} | |
if (nums.size() < 2) return result; | |
int left=summize({nums.begin(),nums.end()-1}); | |
int right=summize({nums.begin()+1,nums.end()}); | |
if (left > result || right > result) return left > right ? left : right; | |
return result; | |
} | |
int maxSubArray(std::vector<int>& nums) | |
{ | |
return summize(nums); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment