题目
在一个长度无限的数轴上,第 i 颗石子的位置为 stones[i]。如果一颗石子的位置最小/最大,那么该石子被称作端点石子。
每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子。
值得注意的是,如果石子像 stones = [1,2,5] 这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子。
当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。
要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案:answer = [minimum_moves, maximum_moves] 。
链接:https://leetcode.com/problems/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:
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.
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.
思路及代码
滑动窗口
class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
stones.sort()
# 初始化sliding window,i为窗口起点,j为窗口终点,n为窗口长度,low记录返回值
i,n,low = 0,len(stones),len(stones)
# high:有几个空位,最多就能挪几次。
# stones[-1]与stones[1]之间的空位数 or stones[-2]与stones[0]之间的空位数
# 比如3,4,5,6,10:4与10之间的空位数为3(7,8,9),3与6之间的空位数为0
high = max(stones[-1]-stones[1]-n+2, stones[-2]-stones[0]-n+2)
# low:有两种情况
# 1. [1,2,4,5,10]:[1,2,4,5]窗口之内数字不连续,且之外还剩一个数字,则只需要一步把10移动到3即可
# 2. [1,2,3,4,10]:[1,2,3,4]窗口之内有n-1个数且数字连续,则需要两步,1移到6,10移到5
for j in range(n):
# 保证窗口长度为n
while stones[j] - stones[i] >= n:
i += 1
# j-i+1 == n-1:window里刚好有n-1个数
# stones[j]-stones[i] == n-2:且这些数都是连续的
if j-i+1 == n-1 and stones[j]-stones[i] == n-2:
low = min(low, 2)
else:
# n-(j-i+1):没有被包含在目前的窗口之内的数字个数
low = min(low, n-(j-i+1))
return [low,high]
复杂度
T
=
O
(
n
)
T = O(n)
T=O(n)
S
=
O
(
n
)
S = O(n)
S=O(n)