aaa
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\n\nclass Solution {\n\n    // 1,2,_,_,5\n    //   2,1,_,5 -> 1,2,5\n    //   2,_,1,5 -> 2,5,1\n\n\n    // 1,2,x,x,x,6\n    //   2,1,x,x,6\n    //     1,2,x,6\n    //       2,1,6\n\n    // 1,2,x,x,x,6\n    //   2,x,x,1,6\n    //   2,6,x,1\n    //     6,2,1\n\n    // a,x,b,x,x,x,c\n    // a,c,b          spaces removed = right + 1\n    //     b,a,x,x,c  spaces removed = left +  1\n\n    // MAX: left/right + 1 always\n    // 3,4,5,6,x,x,x,10\n\n    // MIN:\n    // 3,4,5,6,x ,x ,x ,10\n    //   4,5,6,x ,3 ,x ,10\n    //   4,5,6,10,3\n\n    // a,b,c,x,x,x,x,d  gap = 4   max = 4  min = 2\n    //   b,c,x,a,x,x,d  min = 2; merge island of 1 to main island\n    //   b,c,d,a\n\n    // a,b,x,x,x,x,c,d   min = 2;   merge island of 2 to main island\n    //   b,x,x,a,x,c,d\n    //         a,b,c,d\n\n    // gaps 1,2\n\n    // max = 4\n    //        2       2               3\n    //      C  AAAAAA  BBBBBBBBBBBBBB   DDD\n    //         AAAAAAC BBBBBBBBBBBBBB   DDD\n    //         AAAAAAC BBBBBBBBBBBBBB D DD\n    //         AAAAAAC BBBBBBBBBBBBBBDD D\n    //         AAAAAACDBBBBBBBBBBBBBBDD\n\n    //      C  AAAAAA  BBBBBBBBBBBBBB   DDD    max = 7\n    //      CD AAAAAA  BBBBBBBBBBBBBB   DD\n    //      CDDAAAAAA  BBBBBBBBBBBBBB   D\n    //       DDAAAAAAC BBBBBBBBBBBBBB   D\n    //        DAAAAAAC BBBBBBBBBBBBBBD  D\n    //         AAAAAAC BBBBBBBBBBBBBBDD D\n    //          AAAAAC BBBBBBBBBBBBBBDDAD\n    //          AAAAACDBBBBBBBBBBBBBBDDA\n\n    // MAX STRATEGY depends on OUTER ISLAND SIZE:\n    // 1) outer island > 1   =>  cost = size of gap; we remove outer gap 1 by 1\n    // moreover we can keep rotating the outer island; total cost = sum of all gaps\n    // 2) outer island == 1  =>  cost = 1  and we remove (left/right gap) + 1 spaces\n    // HOWEVER need to only do step 2) ONCE; then we can do step 1!!!!!!!\n\n    // MIN STRATEGY:\n    // 1) try all possible windows of size N; and check how many spaces need to be filled\n\n    public int[] numMovesStonesII(int[] A) {\n        int min = A.length, max = 0, n = A.length;\n        Arrays.sort(A);\n        int leftMostSpace = A[1] - A[0] - 1;\n        int rightMostSpace = A[n - 1] - A[n - 2] - 1; // no need to even loop for the spaces; because the vals are on the X-axis\n        int numsInRange = (A[n - 1] - A[0] + 1); // nums in range INCLUSIVE\n        int spaces = numsInRange - n; // nums in range INCLUSIVE - numbers in range = gaps\n        max = spaces;\n        if (leftMostSpace > 0 && rightMostSpace > 0)\n            max -= Math.min(leftMostSpace, rightMostSpace); // 1 more removed space but cost + 1 too; cancels out\n\n        // 4,7,9\n        for (int l = 0, r = 1; l < n; l++) {\n            while (r < n - 1 && (A[r] - A[l] + 1) < n) r++;\n            int numsInWindowRange = A[r] - A[l] + 1;\n            int stonesInWindow = r - l + 1;\n            int positionsToFill = n - stonesInWindow; // moves needed\n            if (numsInWindowRange > n) {  // window too big, need to shrink it with 1 step;  a,b,x,x,x,x,x,c,d\n                positionsToFill++;\n                stonesInWindow--; // rightmost stone is outside the first N spaces of window\n            }\n            // nums in window == N  => spaces are in MIDDLE\n            // nums in window != N  => spaces in front or at the back; handle single missing elem !!!\n            if (numsInWindowRange != n && stonesInWindow == n - 1) positionsToFill ++; // edge case a,b,x,x,c\n            min = Math.min(min, positionsToFill);\n        }\n        // 4,7,9\n        // 4,x,x,7,x,9\n\n        // 2,5,6,7,8\n        // 2,x,x,5,6,7,8\n\n        return new int[]{min, max};\n    }\n\n}\n```
['Java']