C++ solution 10~20ms
\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& A) {\n        int mx = 0, sz = A.size(), i = A[0], l = 0, r = 0;\n        vector<int>
```\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& A) {\n        int mx = 0, sz = A.size(), i = A[0], l = 0, r = 0;\n        vector<int> res;\n        ios::sync_with_stdio(false);\n        \n        sort(A.begin(), A.end());\n                \n        while(r < sz && A[r] <= A[0]+sz-1) r++;\n        i = A[0]+sz - 1;//right point of window\n        \n        //The count of points outside of the window must be larger than the count of right and left unocuppied point of the window\n        if(l + sz - r == 0 || l + sz - r > ((i+1-sz!=A[l])+(i!=A[r-1])))\n            mx = max(mx, r - l); \n        \n        while(r < sz && i <= A[sz - 1]){\n            //move window\n            i = min(A[l+1] + sz - 1, A[r]);\n            if(i >= A[r]){\n                r++;\n            }\n            if(i+1-sz > A[l])\n                l++;\n            \n            //update max\n            if(l + sz - r == 0 || l + sz - r > ((i-sz+1!=A[l])+(i!=A[r-1])))\n                mx = max(mx, r - l);\n        }\n        res.push_back(sz - mx);\n        res.push_back(A[sz - 1] - A[0] - sz + 1 - min(A[sz - 1] - A[sz - 2] - 1, A[1] - A[0] - 1));\n        return res;\n    }\n};\n```
[]