#1040

Moving Stones Until Consecutive II

specialist · 800 · lc medium +31 · verified · 58.8% accepted · 406 likes · top 55%

Description

Several stones rest at distinct positions on the X-axis given by integer array stones. An endpoint stone is one at the minimum or maximum position. Each move picks an endpoint stone and places it at any unoccupied position that is not an endpoint.

The game ends when no moves remain (stones occupy consecutive positions).

Return [answer[0], answer[1]] where answer[0] is the minimum possible moves and answer[1] is the maximum.

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.

Code

1
2
3