Sunday, June 26, 2016

Ugly Number

Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Solution {
    public boolean isUgly(int num) {
        if(num==0)return false;
        //divid as much as possible to each of 2, 3 and 5 and see if result is 1
        //if result is nnot 1, then it is not ugly number
        while(num%2==0)num=num/2;//num must be multiple times of 2
        while(num%3==0)num=num/3;
        while(num%5==0)num=num/5;
        return num==1;
    }
}
Ugly Number 2
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.
Hint:
  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Lessons learned:
overflow:int to long when int multiply int. special case: n=1
key: priority queue and k sorted list merging.
key: repeated numbers should not be duplicated added. e.g. 6=2*3, 6 will be added twice if not checking.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public int nthUglyNumber(int n) {
        if(n==1)return 1;
        PriorityQueue<Long> pq = new PriorityQueue<Long>();
        pq.add(1l);//first ugly number is 1
        int cnt=0;
        long minUgly=1;
        while(cnt<n){
            minUgly= pq.poll();//access one ugly number
            cnt++;//so counter increase 1
            if(!pq.contains(minUgly*2l))pq.add(minUgly*2l);
            if(!pq.contains(minUgly*3l))pq.add(minUgly*3l);
            if(!pq.contains(minUgly*5l))pq.add(minUgly*5l);

        }
        return (int)minUgly;
    }
}
Super Ugly Numbers: this actually just need to change few lines of code in ugly number 2.

Write a program to find the nth 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 ≤ 106, 0 < primes[i] < 1000.

Note: this implement is not optimized and runs timeout. putting here for demonstrate thought of solution.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
    public int nthSuperUglyNumber(int n, int[] primes) {
        
  if(n==1)return 1;
        PriorityQueue<Long> pq = new PriorityQueue<Long>();
        pq.add(1l);//first ugly number is 1
        int cnt=0;
        long minUgly=1;
        while(cnt<n){
            minUgly= pq.poll();//access one ugly number
            //System.out.println(minUgly);
            cnt++;//so counter increase 1
            for(int p:primes){
             if(!pq.contains(minUgly*p))pq.add(minUgly*p);
            }

        }
        return (int)minUgly;
    }
}

No comments: