package leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * https://leetcode.com/problems/ugly-number-ii/
 */
public class Problem264 {
    public int nthUglyNumber(int n) {
        int x = (int) (Math.log10(n) / Math.log10(3)) + 3;
        Set<Integer> processed = new HashSet<>();
        Set<Integer> uglyNumbers = new TreeSet<>();
        uglyNumbers.add(1);
        List<Integer> level = new ArrayList<>();
        level.add(1);
        for (int i = 0; i < x; i++) {
            List<Integer> tmp = new ArrayList<>();
            for (int num : level) {
                int a = num * 2;
                if (!processed.contains(a)) {
                    tmp.add(a);
                    processed.add(a);
                    uglyNumbers.add(a);
                }
                int b = num * 3;
                if (!processed.contains(b)) {
                    tmp.add(b);
                    processed.add(b);
                    uglyNumbers.add(b);
                }
                int c = num * 5;
                if (!processed.contains(c)) {
                    tmp.add(c);
                    processed.add(c);
                    uglyNumbers.add(c);
                }
            }
            level = tmp;
            System.out.println(level);
        }
        System.out.println(uglyNumbers);
        int i = 1;
        for (int num : uglyNumbers) {
            if (i == n) {
                return num;
            }
            i++;
        }
        return 0;
    }

    public static void main(String[] args) {
        Problem264 prob = new Problem264();
//        System.out.println(prob.nthUglyNumber(7)); // 8
//        System.out.println(prob.nthUglyNumber(9)); // 10
        System.out.println(prob.nthUglyNumber(19)); // 32
//        System.out.println(prob.nthUglyNumber(312));
    }
}

@@ -16,107 +16,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A
 impo
@@ -172,257 +172,79 @@
     
-int x = (int) (Math.log10(n) / Math.log10(3)) + 3;%0A        Set%3CInteger%3E processed = new HashSet%3C%3E();%0A        Set%3CInteger%3E uglyNumbers = new TreeSet%3C%3E();%0A        uglyNumbers.add(1);%0A        List%3CInteger%3E level = new ArrayList%3C%3E();%0A        level.add(1
+TreeSet%3CLong%3E uglyNumbers = new TreeSet%3C%3E();%0A        uglyNumbers.add(1l
 );%0A 
@@ -259,24 +259,25 @@
 or (
-int
+long
  i = 
-0
+1
 ; i %3C 
-x
+n
 ; i+
@@ -297,1326 +297,204 @@
     
-List%3CInteger%3E tmp = new ArrayList%3C%3E();%0A            for (int num : level) %7B%0A                int a = num * 2;%0A                if (!processed.contains(a)) %7B%0A                    tmp.add(a);%0A                    processed.add(a);%0A                    uglyNumbers.add(a);%0A                %7D%0A                int b = num * 3;%0A                if (!processed.contains(b)) %7B%0A                    tmp.add(b);%0A                    processed.add(b);%0A                    uglyNumbers.add(b);%0A                %7D%0A                int c = num * 5;%0A                if (!processed.contains(c)) %7B%0A                    tmp.add(c);%0A                    processed.add(c);%0A                    uglyNumbers.add(c);%0A                %7D%0A            %7D%0A            level = tmp;%0A            System.out.println(level);%0A        %7D%0A        System.out.println(uglyNumbers);%0A        int i = 1;%0A        for (int num : uglyNumbers) %7B%0A            if (i == n) %7B%0A                return num;%0A            %7D%0A            i++;%0A        %7D%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem264 prob = new Problem264();%0A//        System.out.println(prob.nthUglyNumber(7)); // 8%0A//        System.out.println(prob.nthUglyNumber(9)); // 10%0A        System.out.println(prob.nthUglyNumber(19)); // 32%0A//        System.out.println(prob.nthUglyNumber(312)
+long j = uglyNumbers.pollFirst();%0A            uglyNumbers.add(j * 2);%0A            uglyNumbers.add(j * 3);%0A            uglyNumbers.add(j * 5);%0A        %7D%0A        return uglyNumbers.first().intValue(
 );%0A 

Solve problem 264