Moving Stones Until Consecutive II Problem
Description
LeetCode Problem 1040.
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 <= 10^41 <= stones[i] <= 10^9- All the values of
stonesare unique.
Solution
Approach: Sorting + Sliding Window
Sort the stones. Let n be their number. The game ends when they fill n consecutive positions.
Maximum moves. Each move takes an end stone and drops it into an empty spot strictly inside the range, so the range only shrinks. The first move decides which end gap is lost: if we move the leftmost stone, the empty spots between stones[0] and stones[1] can never be used again (the new left end is stones[1]). After that, we can always move the end stone just past its neighbour, so every remaining empty spot is filled one move at a time. The empty spots in [stones[1], stones[n-1]] number stones[n-1] - stones[1] - (n - 2), and symmetrically for moving the rightmost stone first. The maximum is the larger of the two.
Minimum moves. Pick the final window of n consecutive positions. Every stone outside it must move once, so the cost is n minus the number of stones already inside. Slide the window with two pointers: for each right stone stones[j], advance i while stones[j] - stones[i] + 1 > n; then j - i + 1 stones fit in a window of length n.
There is one exception. If n - 1 stones are already consecutive and the last stone is far away (e.g. [1, 2, 3, 4, 10]), the formula says one move, but that move is illegal: moving 10 next to the block would leave it an endpoint. It takes two moves instead (move 1 to 6, then 10 to 5). This case is exactly j - i + 1 == n - 1 with stones[j] - stones[i] == n - 2.
Complexity: O(n log n) time for sorting, and O(log n) extra space.
class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
stones.sort()
n = len(stones)
# Most moves: the first move gives up the gap next to one end; afterwards every empty
# spot between the remaining stones can be used one by one.
most = max(stones[-1] - stones[1], stones[-2] - stones[0]) - (n - 2)
# Fewest moves: slide a window of n consecutive positions; stones outside it must move in.
fewest = n
i = 0
for j in range(n):
while stones[j] - stones[i] + 1 > n:
i += 1
inside = j - i + 1
if inside == n - 1 and stones[j] - stones[i] == n - 2:
# n - 1 stones already consecutive and the last one far away: it takes 2 moves, not 1.
fewest = min(fewest, 2)
else:
fewest = min(fewest, n - inside)
return [fewest, most]