Difficulty: Easy | Acceptance: 79.80% | Paid: No Topics: Math, Simulation
You are given two non-negative integers num1 and num2.
In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.
Return the number of operations required to make either num1 = 0 or num2 = 0.
- Examples
- Constraints
- Simulation
- Optimized Simulation (Euclidean Algorithm)
Examples
Input: num1 = 2, num2 = 3
Output: 3
Explanation:
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1 and get num1 = 2 - 1 = 1, num2 = 1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1 and get num1 = 1 - 1 = 0, num2 = 1.
Input: num1 = 10, num2 = 10
Output: 1
Explanation:
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0, num2 = 10.
Constraints
0 <= num1, num2 <= 10⁵
Simulation
Intuition Directly implement the operation described in the problem statement. Loop until one of the numbers becomes zero, subtracting the smaller value from the larger one in each step and incrementing a counter.
Steps
- Initialize a counter
opsto 0. - Loop while
num1is greater than 0 andnum2is greater than 0. - Inside the loop, check if
num1is greater than or equal tonum2. - If yes, subtract
num2fromnum1. Otherwise, subtractnum1fromnum2. - Increment the
opscounter. - Return
opsonce the loop terminates.
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ops = 0
while num1 > 0 and num2 > 0:
if num1 >= num2:
num1 -= num2
else:
num2 -= num1
ops += 1
return opsComplexity
- Time: O(max(num1, num2)) - In the worst case (e.g., num1=1, num2=10⁵), we perform roughly max(num1, num2) subtractions.
- Space: O(1) - We only use a few variables for storage.
- Notes: This approach is simple but can be slow for large numbers with large differences.
Optimized Simulation (Euclidean Algorithm)
Intuition Instead of subtracting the smaller number from the larger number one by one, we can use division to determine how many times we can subtract the smaller number at once. This is similar to the logic behind the Euclidean algorithm for finding the Greatest Common Divisor (GCD).
Steps
- Initialize a counter
opsto 0. - Loop while
num1is greater than 0 andnum2is greater than 0. - If
num1is greater than or equal tonum2:- Add
num1 / num2(integer division) toops. - Update
num1to benum1 % num2.
- Add
- Else:
- Add
num2 / num1(integer division) toops. - Update
num2to benum2 % num1.
- Add
- Return
ops.
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ops = 0
while num1 > 0 and num2 > 0:
if num1 >= num2:
ops += num1 // num2
num1 %= num2
else:
ops += num2 // num1
num2 %= num1
return opsComplexity
- Time: O(log(min(num1, num2))) - The number of steps is proportional to the number of steps in the Euclidean algorithm.
- Space: O(1) - Constant space is used.
- Notes: This is the most efficient approach, significantly reducing the number of iterations for large inputs.