Why is my Java solution slower?


  • 0
    S

    I'm using two pointers in Java. I thought my solution is simple enough, but found out most Java solutions only took 1ms, while mine took 2ms. Anything I can improve here?

    public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int len = Integer.MAX_VALUE;
        int b = 0, e = 0;
        int sum = 0;
        while(b < nums.length && e < nums.length) {
            sum = sum + nums[e];
            if (sum >= s) {
                len = Math.min(len, e - b + 1);
                sum = sum - nums[b] - nums[e];
                b++;
                e = b > e ? b : e;
            } else {
                e++;
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }
    

    }


Log in to reply
 

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