Moving Stones Until Consecutive II

There are some stones in different positions on the X-axis. You are given an integer array stones, where stones[i] is the position of a stone.

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 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, meaning the stones are in consecutive positions.

Return an integer array answer of length 2 where:

  • answer[0] is the minimum number of moves you can play.
  • answer[1] is the maximum number of moves you can play.
Example 1
Inputstones = [7,4,9]
Output[1,2]
We can move 4 to 8 for one move to finish the game, or move 9 to 5 and then 4 to 6 for two moves.
Example 2
Inputstones = [6,5,4,3,10]
Output[2,3]
We can move 3 to 8 and then 10 to 7 to finish in two moves, or move 3 to 7, 4 to 8, and 5 to 9 to finish in three moves.

Constraints

  • 3 <= stones.length <= 10^4
  • 1 <= stones[i] <= 10^9
  • All the values of stones are unique.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate