264. Ugly Number II
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
Solution
Use Dynamic Programming
The idea of this solution is from this page: http://www.geeksforgeeks.org/ugly-numbers/
The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
(2) 1×2, 2×2, 3×2, 4×2, 5×2, …
(3) 1×3, 2×3, 3×3, 4×3, 5×3, …
(5) 1×5, 2×5, 3×5, 4×5, 5×5, …
We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5.
Then we use similar merge method as merge sort, to get every ugly number from the three subsequence.
Example:
Let us see how it works
initialize
ugly[] = | 1 |
i2 = i3 = i5 = 0;
First iteration
ugly[1] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(2, 3, 5)
= 2
ugly[] = | 1 | 2 |
i2 = 1, i3 = i5 = 0 (i2 got incremented )
Second iteration
ugly[2] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(4, 3, 5)
= 3
ugly[] = | 1 | 2 | 3 |
i2 = 1, i3 = 1, i5 = 0 (i3 got incremented )
Third iteration
ugly[3] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(4, 6, 5)
= 4
ugly[] = | 1 | 2 | 3 | 4 |
i2 = 2, i3 = 1, i5 = 0 (i2 got incremented )
Fourth iteration
ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(6, 6, 5)
= 5
ugly[] = | 1 | 2 | 3 | 4 | 5 |
i2 = 2, i3 = 1, i5 = 1 (i5 got incremented )
Fifth iteration
ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(6, 6, 10)
= 6
ugly[] = | 1 | 2 | 3 | 4 | 5 | 6 |
i2 = 3, i3 = 2, i5 = 1 (i2 and i3 got incremented )
Will continue same way till I < 150
Every step we choose the smallest one, and move one step after,including nums with same value.
Thanks for this author about this brilliant idea. Here is my java solution
public class Solution {
public int nthUglyNumber(int n) {
int index2 = 0, index3 = 0, index5 = 0;
int factor2 = 2, factor3 = 3, factor5 = 5;
int ugly[] = new int[n];
ugly[0] = 1;
for(int i=1; i<n; i++){
int min = Math.min(Math.min(factor2, factor3), factor5);
ugly[i] = min;
if(factor2==min) factor2 = 2*ugly[++index2];
if(factor3==min) factor3 = 3*ugly[++index3];
if(factor5==min) factor5 = 5*ugly[++index5];
}
return ugly[n-1];
}
}
313. Super Ugly Number
Write a program to find the n^th super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.
Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 10^6, 0 < primes[i] < 1000.
public class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
int ugly[] = new int[n];
ugly[0] = 1;
int prim_len = primes.length;
int factor[] = new int[prim_len];
int index[] = new int[prim_len];
for(int i=0; i<prim_len; i++) factor[i] = primes[i];
for(int i=1; i<n; i++){
int min = getArrayMin(factor);
ugly[i] = min;
for(int j=0; j<prim_len; j++){
if(factor[j]==min) factor[j] = primes[j]*ugly[++index[j]];
}
}
return ugly[n-1];
}
int getArrayMin(int nums[]){
int min = Integer.MAX_VALUE;
for(int i=0; i<nums.length; i++){
if(nums[i]<min) min = nums[i];
}
return min;
}
}