Input: [7,4,9]
Output: [1,2]
Explanation:
We can move 4 -> 8 for one move to finish the game.
Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.
Input: [6,5,4,3,10]
Output: [2,3]
We can move 3 -> 8 then 10 -> 7 to finish the game.
Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
Input: [100,101,104,102,103]
Output: [0,0]
class Solution {
public:
static const int INF = 1e9 + 5;
vector numMovesStonesII(vector& stones) {
sort(stones.begin(), stones.end());
int n = stones.size();
int least = INF, most = -INF;
for (int i = 0, j = 0; i < n; i++) {
while (j + 1 < n && stones[j + 1] - stones[i] < n)
j++;
int now = n - (j - i + 1);
if (j - i == n - 2 && stones[j] - stones[i] == j - i)
now++;
least = min(least, now);
}
most = max(stones[n - 1] - stones[1], stones[n - 2] - stones[0]) - (n - 2);
return {least, most};
}
};