Two pointers solution, Java, why TLE


  • 0
    J

    I do not understand why I encountered TLE with this solution

    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            if(nums == null || nums.length == 0) {
                return 0;
            }
            int ans = Integer.MAX_VALUE;
            int i = 0;
            int j = 0;
            int sum = 0;
            for(i = 0; i < nums.length; i++) {
                while(j < nums.length) {
                    if (sum < s) {
                        sum += nums[j];
                        j++;
                    } else {
                        break;
                    }
                }
                if(sum >= s) {
                    ans = Math.min(ans, j - i);
                }
                sum -= nums[i];
            }
            return ans == Integer.MAX_VALUE ? 0 : ans;
        }
    }
    

Log in to reply
 

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