[Python] Sliding window with detailed expalanation
python\nclass Solution:\n    def numMovesStonesII(self, stones: list[int]) -> list[int]:\n        """\n        1. For the higher bound, it is determined by eith
```python\nclass Solution:\n    def numMovesStonesII(self, stones: list[int]) -> list[int]:\n        """\n        1. For the higher bound, it is determined by either moving the leftmost\n            to the right side, or by moving the rightmost to the left side:\n            1.1 If moving leftmost to the right side, the available moving\n                positions are A[n - 1] - A[1] + 1 - (n - 1) = \n                A[n - 1] - A[1] - n + 2\n            1.2 If moving rightmost to the left side, the available moving\n                positions are A[n - 2] - A[0] + 1 - (n - 1) = \n                A[n - 2] - A[0] - n + 2.\n        2. For the lower bound, we could use sliding window to find a window\n            that contains the most consecutive stones (A[i] - A[i - 1] = 1):\n            2.1 Generally the moves we need are the same as the number of\n                missing stones in the current window.\n            2.3 When the window is already consecutive and contains all the\n                n - 1 stones, we need at least 2 steps to move the last stone\n                into the current window. For example, 1,2,3,4,10:\n                2.3.1 We need to move 1 to 6 first as we are not allowed to\n                    move 10 to 5 as it will still be an endpoint stone.\n                2.3.2 Then we need to move 10 to 5 and now the window becomes\n                    2,3,4,5,6.\n        """\n        A, N = sorted(stones), len(stones)\n        maxMoves = max(A[N - 1] - A[1] - N + 2, A[N - 2] - A[0] - N + 2)\n        minMoves = N\n\n        # Calculate minimum moves through sliding window.\n        start = 0\n        for end in range(N):\n            while A[end] - A[start] + 1 > N:\n                start += 1\n\n            if end - start + 1 == N - 1 and A[end] - A[start] + 1 == N - 1:\n                # Case: N - 1 stones with N - 1 positions.\n                minMoves = min(minMoves, 2)\n            else:\n                minMoves = min(minMoves, N - (end - start + 1))\n\n        return [minMoves, maxMoves]\n```
['Sliding Window', 'Python3']