[
greedy
brainteaser
]
Leetcode 1033. Moving Stones Until Consecutive
Problem statement
https://leetcode.com/problems/moving-stones-until-consecutive/
Solution
First of all, value max - min
will change at least by one on each step, so maximum number of steps is c - a - 2
, where we sorted values. Also it is always enough to make no more than 2
steps to make them consecutive,If they already consecutive, nubmer of steps is 0
. If some pair is consecutive or have gap 2
, then number of steps is 1
, else it is 2
.
Complexity
It is O(1)
for time and space.
Code
class Solution:
def numMovesStones(self, a, b, c):
a, b, c = sorted([a, b, c])
mx = c - a - 2
mn = 0 if c - a == 2 else 1 if b - a <= 2 or c - b <= 2 else 2
return [mn, mx]