class Solution { public: int maxSubArray(vector<int>& nums) { int n = nums.size(), lMax = 0, gMax = nums[0]; for(int i = 0; i < n; ++i) { lMax += nums[i]; gMax = max(gMax, lMax); if(lMax<0) lMax = 0; } return gMax; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter