Simplest Solution in Java


  • 0
    R
    public class Solution {
        public int maxSubArray(int[] nums) {
            int max = Integer.MIN_VALUE;
            int curr_max = 0;
            for (int i : nums) {
                curr_max+=i;
                max = Math.max(curr_max, max);
                if (curr_max < 0) {
                    curr_max = 0;
                }
            }
            return max;
        }
    }
    

Log in to reply
 

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