You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This discussion was converted from issue #22 on September 15, 2026 10:59.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://leetcode.com/problems/nth-magical-number/
Problem Summary
a 또는 b로 나누어 지는 수를 magical number라 할 때, n번째 magical number를 구하는 문제
Solution
일단 n이 크니까 탐색 범위를 줄여보자
LCM을 쓰면 a, b에 동시에 나누어지는 수를 구할 수 있고 이를 이용해 자르면 된다.
자른 다음 나머지 n에 대해 하나씩 a, b를 더해가면서 n번째 수를 구할 수 있다.
솔루션을 보면 이진 탐색으로 구하면 더 빠르게 구할 수 있다. x라는 수가 몇 번째인지 구할 수 있기 때문이다. (x // a + x // b - x // lcm). n번째의 최소의 x를 구하면 된다.
Source Code
All reactions