C++ - DP - Kadane's Algorithm


  • 0
     int maxSubArray(vector<int>& nums) {
            int curr_max =nums[0];
            int max_so_far = nums[0];
            
            for(int i=1; i<nums.size();i++){
                curr_max = max(curr_max+nums[i], nums[i]);
                max_so_far = max(max_so_far, curr_max);
            }
            
            return max_so_far;
        }

Log in to reply
 

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