## # 1040. Moving Stones Until Consecutive II

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]
``````

### # Solution

Approach 1: try a few examples and figure out what the max and min number of moves mean.

### # Code (Python)

Approach 1:

``````class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
stones.sort()
if stones[-1] - stones + 1 == len(stones): # consecutive
return [0, 0]
return [self._find_min(stones), self._find_max(stones)]

def _find_min(self, stones):
# for each window of size len(stones), find the number of moves to put all stones inside that window (has to do with how many existing stones there are in the window).
# 1 empty slot == 1 move, except the edge case where all stones are consecutive except for 1 outlier and an endpoint.
global_min = float('inf')
left_bound = 0
for right_bound in range(1, len(stones)):
while stones[right_bound] - stones[left_bound] >= len(stones): # should be len(A) - 1 if window size == len(A)
left_bound += 1
if stones[right_bound] - stones[left_bound]  == len(stones) - 2 and right_bound - left_bound + 1 == len(stones) - 1: # left and right bounds a consecutive, except for 1 outlier
global_min = min(global_min, 2)
else:
global_min = min(global_min, len(stones) - (right_bound - left_bound + 1)) # the number of empty slots
return global_min

def _find_max(self, stones):
# Scoot all stones to the left side of the right endpoint stone, or to the right side of the left endpoint stone.
# To scoot to the right, first use 1 move to bring A and A together and become (A, A + 1), then use 1 move each to scoot all stones to the right. Leftmost endpoint starts from A and ends at A[-1] - len(stones) + 1.
return max(1 + (stones[-1] - len(stones) + 1) - stones, 1 + stones[-2] - (stones + len(stones) - 1))
``````

Approach 1:

Approach 2: