There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.
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 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 (i.e., the stones are in three consecutive positions).
Return an integer arrayanswerof length2where :
answer[0]is the minimum number of moves you can play, and
answer[1]is the maximum number of moves you can play.
Input: stones =[7,4,9] Output:[1,2] Explanation: We can move 4->8for one move to finish the game. Or, we can move 9->5,4->6for two moves to finish the game.
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.
Sort the stones. The minimum moves is the smallest number of moves to fill a window of size n with stones, except for the special case where all but one stone are consecutive and the last stone is two away. The maximum moves is the largest gap minus n plus 2.
Sort stones. For min moves, use a sliding window of size n and count how many stones are in the window. The minimum moves is n minus the max stones in any window, except for the special case where the window is n-1 stones and the last stone is two away (then min moves is 2). For max moves, it’s max(stones[n-1]-stones[1], stones[n-2]-stones[0]) - n + 2.