Java BFS exceed time limit


  • 0
    D

    public class Solution {

    public int minSubArrayLen(int s, int[] nums) {
        LinkedList<Stat> queue = new LinkedList<>();
        queue.add(new Stat(0, 0, 0));
        //bfs
        while (!queue.isEmpty()) {
            Stat pre = queue.poll();
            for (int i = pre.begin; i < nums.length; i++) {
                int sum = pre.sum + nums[i];
                int cnt = pre.cnt + 1;
                if (sum == s) return pre.cnt+1;
                queue.add(new Stat(i+1, sum, cnt));
            }
        }
        return 0;
    }
    
    class Stat {
        int begin;
        int sum;
        int cnt;
    
        public Stat(int begin, int sum, int cnt) {
            this.begin = begin;
            this.sum = sum;
            this.cnt = cnt;
    
        }
    }
    

    }


Log in to reply
 

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