[C++] lots of subtle corner cases, learned from forum
\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        const int n = stones.size();\n        std::sort(stones.begin(), st
```\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        const int n = stones.size();\n        std::sort(stones.begin(), stones.end());\n\n        int j = 0;\n        for (; j < n && stones[j] - stones[0] + 1 <= n; ++j) {}\n        int maxOccupied = j;\n        for (int i = 0; j < n; ++j) {\n            while (stones[j] - stones[i] + 1 > n) { ++i; }\n            maxOccupied = std::max(maxOccupied, j - i + 1);\n        }\n        int minimumMoves = n - maxOccupied;\n        if (stones[n - 2] - stones[0] + 1 == n - 1 && stones[n - 1] > stones[n - 2] + 2) {\n            minimumMoves = 2;\n        }\n        if (stones[n - 1] - stones[1] + 1 == n - 1 && stones[1] > stones[0] + 2) {\n            minimumMoves = 2;\n        }\n\n        const int spaces = stones.back() - stones.front() + 1 - n;\n        const int maximumMoves = std::max(spaces - (stones[1] - stones[0] - 1),\n            spaces - (stones.back() - stones[n - 2] - 1));\n        return {minimumMoves, maximumMoves};\n    }\n};\n```
[]