# 1033. Moving Stones Until Consecutive
Three stones are on a number line at positions a, b, and c.
Each turn, let's say the stones are currently at positions x, y, 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, 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: a = 1, b = 2, c = 5
Output: [1, 2]
Explanation: Move stone from 5 to 4 then to 3, or we can move it directly to 3.
Example 2:
Input: a = 4, b = 3, c = 2
Output: [0, 0]
Explanation: We cannot make any moves.
# Solution
Approach 1: try a few examples and figure out what the max and min number of moves mean.
# Code (Python)
Approach 1:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
a, b, c = sorted([a, b, c])
if a + 1 == b and b + 1 == c:
return [0, 0]
max_moves = c - a - 2 # for all cases
# two consecutives or there's 1 empty slot between any 2 stones
if (a + 1 == b or b + 1 == c) or (a + 2 == b or b + 2 == c):
min_moves = 1
else:
min_moves = 2
return [min_moves, max_moves]
# Code (C++)
Approach 1:
Approach 2: