My 13-line 8ms solution


  • 0
    C
    class Solution {
    public:
    	int maxSubArray(vector<int>& nums) {
    		if (nums.empty())
    			return 0;
    		int acc = max(0, nums[0]), sum = nums[0];
    		for (int i = 1; i < nums.size(); i++) {
    			sum = max(sum, acc + nums[i]);
    			acc = max(0, acc + nums[i]);
    		}
    		return sum;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.