264. Ugly Number II
Problem
Approach
- Firstly, it is easy to check if every number is ugly number. But, it costs too much time in computing these un-ugly numbers and it will result
TLE
. - So, let’s figure out another method. Obviously, all ugly numbers are 2i3j4k. Then, we can generate new ugly numbers by tracking if some ugly number has been multiplied by
2
,3
and5
, which can be tracked by 3 pointersp2 p3 & p5
. - For example, the init ugly numbers are
[1]
andpointer2
,pointer3
andpointer5
are refering to index0
. Then by comparing1 * 2
,1 * 3
and1 * 5
, which areugly[pointer2] * 2, ugly[pointer3] * 3 and ugly[pointer5] * 5
, we can get1 * 2
is minimal andpointer2
can be moved to next index(pointer2 ++
) and the new ugly numbers are[1,2]
. In the same way, by comparing2 * 2
,1 * 3
and1 * 5
,1 * 3
is minimal andpointer3
will be moved to next index and the new ugly numbers are[1,2,3]
. And so on…
Code
class Solution {
public int nthUglyNumber(int n) {
int[] dp = new int[n];
dp[0] = 1;
int p2 = 0, p3 = 0, p5 = 0;// pi stands for the number, dp[pi], which is not mulpiplied by i, i = 2,3 or 5
for(int i = 1; i < n; i ++){
int min = Math.min(dp[p2] * 2, Math.min(dp[p3] * 3, dp[p5] * 5));
if(min == dp[p2] * 2) p2 ++;
if(min == dp[p3] * 3) p3 ++;
if(min == dp[p5] * 5) p5 ++;
dp[i] = min;
}
return dp[n - 1];
}
}