MediumNo. 1033
Moving Stones Until Consecutive — LeetCode 1033 Python Solution
- Pattern
- Math and Number Theory
- Topics
- Brainteaser, Math
- Reading time
- 2 min
- Source
- leetcode.com
The problem
There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.
Example
- Input
- a = 1, b = 2, c = 5
- Output
- [1,2]
- Explanation
- Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
Python solution
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
x, z = min(a, b, c), max(a, b, c)
y = a + b + c - x - z
mi = mx = 0
if z - x > 2:
mi = 1 if y - x < 3 or z - y < 3 else 2
mx = z - x - 2
return [mi, mx]Complexity
- Time
- O(n) or O(1)
- Space · auxiliary
- O(1)
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1033. Moving Stones Until Consecutive is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1040 | Moving Stones Until Consecutive II | Medium |
| 0292 | Nim Game | Easy |
| 0319 | Bulb Switcher | Medium |
| 2396 | Strictly Palindromic Number | Medium |
| 1037 | Valid Boomerang | Easy |
| 1041 | Robot Bounded In Circle | Medium |
Frequently asked questions
- How hard is LeetCode 1033. Moving Stones Until Consecutive?
LeetCode 1033. Moving Stones Until Consecutive is rated Medium on LeetCode.
- What topics does LeetCode 1033. Moving Stones Until Consecutive cover?
LeetCode 1033. Moving Stones Until Consecutive is tagged Brainteaser and Math on LeetCode.