Moving Stones Until Consecutive Problem
Description
LeetCode Problem 1033.
There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.
In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions x, y, and z with x < y < z. You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.
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: a = 1, b = 2, c = 5
Output: [1,2]
Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
Example 2:
Input: a = 4, b = 3, c = 2
Output: [0,0]
Explanation: We cannot make any moves.
Example 3:
Input: a = 3, b = 5, c = 1
Output: [1,2]
Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
Constraints:
1 <= a, b, c <= 100a,b, andchave different values.
Solution
Approach: Case Analysis on the Gaps
Sort the positions so that x < y < z. If z - x == 2, the stones are already consecutive and the answer is [0, 0].
Minimum. Otherwise at least one move is needed, and two moves are always enough: move x to y - 1 and z to y + 1 (skipping a move if that side is already next to y). One move is enough when either gap is small:
y - x <= 2(the left pair is adjacent or has exactly one empty spot between them): moveznext to the pair or into the spot. The same holds forz - y <= 2.- If both gaps are larger than
2, a single move cannot make all three stones consecutive, so the minimum is2.
The case with exactly one empty spot, like [1, 3, 10], is easy to miss: moving 10 to 2 finishes in one move.
Maximum. Every move puts an endpoint stone strictly inside the span, so the empty spots between x and z can only be used up, and each move uses up at least one of them. There are z - x - 2 such spots, so no game lasts longer than z - x - 2 moves. That many moves is also possible: always move an endpoint just one step inward, onto the empty spot next to it (move x to x + 1, or z to z - 1 when x is already next to y). Each such move uses up exactly one spot, so the maximum is z - x - 2.
Complexity: O(1) time and space.
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
x, y, z = sorted((a, b, c))
if z - x == 2:
return [0, 0] # already consecutive
# One move is enough when two stones are adjacent (move the third next to them)
# or have exactly one gap between them (move the third into the gap).
fewest = 1 if y - x <= 2 or z - y <= 2 else 2
# Each move can shrink the span by just one: every empty spot between x and z costs one move.
most = z - x - 2
return [fewest, most]