n and t. Return the smallest number greater than or equal to n such that the product of its digits is divisible by t.
Example 1:
Output: 10
Explanation:
The digit product of 10 is 0, which is divisible by 2, making it the smallest number greater than or equal to 10 that satisfies the condition.
Example 2:
Output: 16
Explanation:
The digit product of 16 is 6, which is divisible by 3, making it the smallest number greater than or equal to 15 that satisfies the condition.
Constraints:
1 <= n <= 1001 <= t <= 10수학, 완전 탐색
이 문제는 두 정수 n과 t가 주어졌을 때, n보다 크거나 같은 가장 작은 정수를 찾는 것입니다. 조건은 해당 정수의 각 자릿수의 곱이 t로 나누어떨어져야 한다는 것입니다.
n (1 ≤ n ≤ 100), 정수 t (1 ≤ t ≤ 10)예를 들어 n=15, t=3일 때:
완전 탐색 (Brute Force) 방식을 사용했습니다.
이유는 다음과 같습니다:
n의 최댓값이 100이고, t의 최댓값이 10입니다.t로도 나누어떨어집니다.n부터 1씩 증가시키며 조건을 확인하는 직관적인 로직으로 충분합니다.자릿수 곱을 계산할 때 문자열로 변환하여 각 문자를 순회하는 방식을 사용했는데, 이는 파이썬에서 가장 간결하고 가독성이 좋은 방법입니다.
while True:
n부터 시작하여 조건을 만족하는 숫자를 찾을 때까지 계속 증가시킵니다.check = 1
for num in str(n):
check *= int(num)
if check % t == 0:
return n
check 변수에 자릿수 곱을 누적합니다.check % t == 0이 되면 즉시 n을 반환합니다.check가 0이 되어 즉시 조건을 만족하므로 빠르게 처리됩니다.n += 1
n을 1 증가시켜 다음 숫자를 검사합니다.시간 복잡도: O(K × D)
K: 정답이 될 때까지 검사하는 숫자의 개수 (최악의 경우 상수 수준, n ≤ 100)D: 숫자의 자릿수 (최대 3자리)공간 복잡도: O(D)
str(n)로 문자열 변환 시 자릿수만큼의 공간 사용 (최대 3문자)class Solution:
def smallestNumber(self, n: int, t: int) -> int:
while True:
check = 1
for num in str(n):
check *= int(num)
if check % t == 0:
return n
n += 1