Why is there a TLE issue with my code?


  • 0
    G

    I know that I did not predicate whether best<0 each time, but how the time complexity various so much?

    	public int maxSubArray(int[] nums) {
    	    int n = nums.length;
            int max = nums[0];
            int best = nums[0];
            for(int i = 1; i < n; i++){
                //best must be update each time!
                best = Math.max(best+nums[i],nums[i]);
                max = Math.max(max, best);
            }
            return max;
        }
    '''

Log in to reply
 

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