Leetcode #1999: Smallest Greater Multiple Made of Two Digits
In this guide, we solve Leetcode #1999 Smallest Greater Multiple Made of Two Digits in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
Given three integers, k, digit1, and digit2, you want to find the smallest integer that is: Larger than k, A multiple of k, and Comprised of only the digits digit1 and/or digit2. Return the smallest such integer.
Quick Facts
- Difficulty: Medium
- Premium: Yes
- Tags: Math, Enumeration
Intuition
There is a mathematical invariant or formula that directly leads to the result.
Using math avoids unnecessary loops and reduces complexity.
Approach
Derive the formula or update rule, then compute the answer directly.
Handle edge cases like overflow or zero carefully.
Steps:
- Identify the math relationship.
- Compute the result with a loop or formula.
- Handle edge cases.
Example
Input: k = 2, digit1 = 0, digit2 = 2
Output: 20
Explanation:
20 is the first integer larger than 2, a multiple of 2, and comprised of only the digits 0 and/or 2.
Python Solution
class Solution:
def findInteger(self, k: int, digit1: int, digit2: int) -> int:
if digit1 == 0 and digit2 == 0:
return -1
if digit1 > digit2:
return self.findInteger(k, digit2, digit1)
q = deque([0])
while 1:
x = q.popleft()
if x > 2**31 - 1:
return -1
if x > k and x % k == 0:
return x
q.append(x * 10 + digit1)
if digit1 != digit2:
q.append(x * 10 + digit2)
Complexity
The time complexity is O(n) or O(1). The space complexity is O(1).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.