Skip to content

Commit 7d75351

Browse files
authored
Create moving-stones-until-consecutive-ii.py
1 parent 1b92219 commit 7d75351

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def numMovesStonesII(self, stones):
6+
"""
7+
:type stones: List[int]
8+
:rtype: List[int]
9+
"""
10+
stones.sort()
11+
left, min_moves = 0, len(stones)
12+
max_moves = max(stones[-1]-stones[1], stones[-2]-stones[0]) - (len(stones)-2)
13+
for right in xrange(len(stones)):
14+
while stones[right]-stones[left]+1 > len(stones): # find window size <= len(stones)
15+
left += 1
16+
if len(stones)-(right-left+1) == 1 and stones[right]-stones[left]+1 == len(stones)-1:
17+
min_moves = min(min_moves, 2) # case (1, 2, 3, 4), 7
18+
else:
19+
min_moves = min(min_moves, len(stones)-(right-left+1)) # move stones not in this window
20+
return [min_moves, max_moves]

0 commit comments

Comments
 (0)