1040. Moving Stones Until Consecutive II
Description
There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.
Call a stone an endpoint stone if it has the smallest or largest position. In one move, 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 position5, since moving it to any position (such as0, or3) will still keep that stone as an endpoint stone.
The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
Return an integer array answer of length 2 where:
answer[0]is the minimum number of moves you can play, andanswer[1]is the maximum number of moves you can play.
Example 1:
Input: stones = [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: stones = [6,5,4,3,10] Output: [2,3] Explanation: 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.
Constraints:
3 <= stones.length <= 1041 <= stones[i] <= 109- All the values of
stonesare unique.
Solutions
Solution 1
Thinking
Only endpoint stones may move onto a non-endpoint vacancy. \(n\le 10^4\) and coordinates up to \(10^9\) rule out a step-by-step search. The maximum is sliding endpoints as slowly as possible; the minimum is packing stones into some window of length \(n\).
After sorting, moving the left end first skips the gap after \(stones[0]\) and yields at most \(stones[n-1]-stones[1]+1-(n-1)\) moves; the right end is symmetric. The minimum uses two pointers on windows of \(n\) slots; a near-full window that still misses one stone needs two moves.
One sort and one sliding window produce \([\textit{mi},\textit{mx}]\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |