##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1040.html

# 1040. Moving Stones Until Consecutive II

Medium

## Description

On an infinite number line, the position of the i-th stone is given by stones[i]. Call a stone an endpoint stone if it has the smallest or largest position.

Each turn, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.

In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.

The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.

When the game ends, what is the minimum and maximum number of moves that you could have made? Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]

Example 1:

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.

Example 2:

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.

Example 3:

Input: [100,101,104,102,103]

Output: [0,0]

Note:

1. 3 <= stones.length <= 10^4
2. 1 <= stones[i] <= 10^9
3. stones[i] have distinct values.

## Solution

To calculate the maximum number of moves, first calculate the number of empty positions between the leftmost stone and the rignt most stone as stones[stones.length - 1] - stones[0] + 1 - stones.length, and calculate the minimum unavailable empty positions as Math.min(stones[1] - stones[0] - 1, stones[stones.length - 1] - stones[stones.length - 2] - 1). The maximum number of moves is calculated as the number of empty positiosn minus the minimum unavailable empty positions.

To calculate the minimum number of moves, find a window of size stones.length that contains the most stones, and move the stones from outside the window to inside the window. For the special case that j - i + 1 == stones.length - 1 && stones[j] - stones[i] + 1 == stones.length - 1, the minimum number of moves is 2.

• class Solution {
public int[] numMovesStonesII(int[] stones) {
Arrays.sort(stones);
int length = stones.length;
int max = stones[length - 1] - stones[0] + 1 - length;
max -= Math.min(stones[1] - stones[0] - 1, stones[length - 1] - stones[length - 2] - 1);
int min = max;
int j = 0;
for (int i = 0; i < length; i++) {
while (j + 1 < length && stones[j + 1] - stones[i] + 1 <= length)
j++;
int cost = length - (j - i + 1);
if (j - i + 1 == length - 1 && stones[j] - stones[i] + 1 == length - 1)
cost = 2;
min = Math.min(min, cost);
}
return new int[]{min, max};
}
}

• // OJ: https://leetcode.com/problems/moving-stones-until-consecutive-ii/
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
vector<int> numMovesStonesII(vector<int>& A) {
sort(begin(A), end(A));
int N = A.size(), mx = A[N - 1] - A[0] - N - min(A[N - 1] - A[N - 2], A[1] - A[0]) + 2, mn = mx;
for (int i = 0, j = 0; i < N; ++i) {
while (j < N && A[j] <= A[i] + N - 1) ++j;
int cnt = N - j + i;
if (cnt == 1 && A[j - 1] != A[i] + N - 1) continue; // If the opening is at the end of the window, skip
mn = min(mn, cnt);
}
return { mn, mx };
}
};

• print("Todo!")