O(n) O(n)
class Solution:
def nthUglyNumber(self, n: int) -> int:
if n <= 0:
return -1
dp,a,b,c = [1] * n,0 ,0 ,0
for i in range(1,n):
n2,n3,n5 =dp[a] * 2,dp[b] * 3,dp[c] * 5
dp[i] = min(n2,n3,n5)
if dp[i] == n2:
a += 1
if dp[i] == n3:# 注意不为elif的原因:n2,n3,n5为首个乘以2/3/5后大于dp[i]的丑数。如果遇到6,a,b要同时加一
b += 1
if dp[i] == n5:
c += 1
return dp[-1]