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 cannotmove 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]
Note:
3 <= stones.length <= 10^4
1 <= stones[i] <= 10^9
stones[i]
have distinct values.
在一个长度无限的数轴上,第 i
颗石子的位置为 stones[i]
。如果一颗石子的位置最小/最大,那么该石子被称作端点石子。
每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子。
值得注意的是,如果石子像 stones = [1,2,5]
这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子。
当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。
要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案:answer = [minimum_moves, maximum_moves]
。
示例 1:
输入:[7,4,9] 输出:[1,2] 解释: 我们可以移动一次,4 -> 8,游戏结束。 或者,我们可以移动两次 9 -> 5,4 -> 6,游戏结束。
示例 2:
输入:[6,5,4,3,10] 输出:[2,3] 解释: 我们可以移动 3 -> 8,接着是 10 -> 7,游戏结束。 或者,我们可以移动 3 -> 7, 4 -> 8, 5 -> 9,游戏结束。 注意,我们无法进行 10 -> 2 这样的移动来结束游戏,因为这是不合要求的移动。
示例 3:
输入:[100,101,104,102,103] 输出:[0,0]
提示:
3 <= stones.length <= 10^4
1 <= stones[i] <= 10^9
stones[i]
的值各不相同。
1 class Solution { 2 func numMovesStonesII(_ stones: [Int]) -> [Int] { 3 var stones = stones.sorted(by:<) 4 var mx:Int = 0 5 var n:Int = stones.count 6 for i in 1..<n 7 { 8 mx += stones[i] - stones[i - 1] - 1 9 } 10 if mx == 0 {return [0,0]} 11 mx -= min(stones[1] - stones[0] - 1, stones[n - 1] - stones[n - 2] - 1) 12 var res:Int = 0 13 if (stones[n - 2] - stones[0] == n - 2 && stones[n - 1] - stones[n - 2] > 2) || (stones[n - 1] - stones[1] == n - 2 && stones[1] - stones[0] > 2) 14 { 15 return [2, mx] 16 } 17 else 18 { 19 var dq:[Int] = [Int]() 20 for stone in stones 21 { 22 dq.append(stone) 23 while(dq.last! - dq.first! + 1 > n) 24 { 25 dq.removeFirst() 26 } 27 res = max(res, dq.count) 28 } 29 } 30 return [n - res, mx] 31 } 32 }