Ugly Number II
Input:
n = 10
Output:
12
Explanation:
1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.class Solution:
def nthUglyNumber(self, n: int) -> int:
i2=i3=i5=0
dp = [0]*n
dp[0]=1
for i in range(1,n):
dp[i] = min(dp[i2]*2,dp[i3]*3,dp[i5]*5)
if dp[i] == dp[i2]*2:
i2+=1
if dp[i] == dp[i3]*3:
i3+=1
if dp[i] == dp[i5]*5:
i5+=1
return dp[n-1]Last updated