Skip to content

Commit bf9d02c

Browse files
authored
Update moving-stones-until-consecutive.py
1 parent 9532807 commit bf9d02c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/moving-stones-until-consecutive.py

+21
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ def numMovesStones(self, a, b, c):
1414
if s[0]+1 == s[1] and s[1]+1 == s[2]:
1515
return [0, 0]
1616
return [1 if s[0]+2 >= s[1] or s[1]+2 >= s[2] else 2, s[2]-s[0]-2]
17+
18+
19+
# Time: O(1)
20+
# Space: O(1)
21+
class Solution2(object):
22+
def numMovesStones(self, a, b, c):
23+
"""
24+
:type a: int
25+
:type b: int
26+
:type c: int
27+
:rtype: List[int]
28+
"""
29+
stones = [a, b, c]
30+
stones.sort()
31+
left, min_moves = 0, float("inf")
32+
max_moves = (stones[-1]-stones[0]) - (len(stones)-1)
33+
for right in xrange(len(stones)):
34+
while stones[right]-stones[left]+1 > len(stones): # find window size <= len(stones)
35+
left += 1
36+
min_moves = min(min_moves, len(stones)-(right-left+1)) # move stones not in this window
37+
return [min_moves, max_moves]

0 commit comments

Comments
 (0)