package com.fishercoder.solutions;

/**
 * 313. Super Ugly Number
 *
 * 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.
 * (4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
 */
public class _313 {

    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] ret = new int[n];
        ret[0] = 1;

        int[] indexes = new int[primes.length];

        for (int i = 1; i < n; i++) {
            ret[i] = Integer.MAX_VALUE;

            for (int j = 0; j < primes.length; j++) {
                ret[i] = Math.min(ret[i], primes[j] * ret[indexes[j]]);
            }

            for (int j = 0; j < indexes.length; j++) {
                if (ret[i] == primes[j] * ret[indexes[j]]) {
                    indexes[j]++;
                }
            }
        }

        return ret[n - 1];
    }

}

@@ -387,16 +387,19 @@
 size 4.%0A
+ *%0A
  * Note:
@@ -674,16 +674,56 @@
 _313 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -775,24 +775,28 @@
 ) %7B%0A        
+  
+  
 int%5B%5D ret = 
@@ -811,24 +811,28 @@
 n%5D;%0A        
+    
 ret%5B0%5D = 1;%0A
@@ -836,24 +836,28 @@
 1;%0A%0A        
+ 
+   
 int%5B%5D indexe
@@ -885,16 +885,20 @@
 ngth%5D;%0A%0A
+    
         
@@ -931,32 +931,36 @@
 ) %7B%0A            
+    
 ret%5Bi%5D = Integer
@@ -976,32 +976,36 @@
 E;%0A%0A            
+   
+ 
 for (int j = 0; 
@@ -1022,32 +1022,36 @@
 .length; j++) %7B%0A
+    
                 
@@ -1110,35 +1110,43 @@
 %5D);%0A            
+  
+  
 %7D%0A%0A
+    
             for 
@@ -1200,16 +1200,20 @@
         
+    
 if (ret%5B
@@ -1245,24 +1245,28 @@
 exes%5Bj%5D%5D) %7B%0A
+    
             
@@ -1295,34 +1295,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -1327,35 +1327,42 @@
       %7D%0A        
-%7D%0A%0A
+    %7D%0A    
         return r
@@ -1372,16 +1372,26 @@
 n - 1%5D;%0A
+        %7D%0A
     %7D%0A%0A%7D

refactor 313