[Java] BFS
The critical point is that we need to remove the possible idx from treeset instead of iterate over them. I was thinking about using a linkedlist but random inde
The critical point is that we need to remove the possible idx from `treeset` instead of iterate over them. I was thinking about using a `linkedlist` but random index access is of O(N) time complexity, and `arraylist` spent O(N) when removing any element.  \nI also learnt about NavigableSet, it helps get the subset of the `treeset`, and once we found the first element, the following random access is O(1) each step, it doesn\'t decrease the overall time complexity though.\n```\nimport java.util.NavigableSet;\n\nclass Solution {\n    public int[] minReverseOperations(int n, int p, int[] banned, int k) {\n        boolean[] skip = new boolean[n];\n        for (int ban : banned)\n            skip[ban] = true;\n        int[] ans = new int[n];\n        Arrays.fill(ans, -1);\n        \n        Deque<Integer> q = new LinkedList<>();\n        TreeSet<Integer> odd = new TreeSet<>();\n        TreeSet<Integer> even = new TreeSet<>();\n        for (int i = 0; i < n; i++)\n            if (i % 2 == 0 && !skip[i] && i != p)\n                even.add(i);\n            else if (i % 2 == 1 && !skip[i] && i != p)\n                odd.add(i);\n        \n        q.offer(p);\n        int cnt = 0;\n        while (!q.isEmpty())    {\n            int size = q.size();\n            while (size-- > 0)  {\n                int x = q.poll();\n                ans[x] = cnt;\n                int minArrStart = Math.max(0, x - k + 1);\n                int maxArrEnd = Math.min(n - 1, x + k - 1);\n                int lo = 2 * minArrStart + k - x - 1;\n                int hi = 2 * maxArrEnd - x - k + 1;\n                TreeSet<Integer> set;\n                set = lo % 2 == 0 ? even : odd;\n                NavigableSet<Integer> subSet = set.subSet(lo, true, hi, true); \n                Iterator<Integer> iterator = subSet.iterator();\n                while (iterator.hasNext()) {\n                    Integer nxt = iterator.next();\n                    q.offer(nxt);\n                    iterator.remove();\n                }\n            }\n            cnt++;\n        }\n        \n        return ans;\n    }\n}\n```
[]