[JAVA] DP (Tabulation) Solution - 2 Methods
1. By creating a Pointer array (Almost same as Ugly number Problem)\n\tTime Complexity - O(NK)\n\tSpace Complexity - O(N+K)\n\tRuntime - 279ms - Faster than 65.
**1. By creating a Pointer array (Almost same as Ugly number Problem)**\n\tTime Complexity - O(NK)\n\tSpace Complexity - O(N+K)\n\tRuntime - 279ms - Faster than 65.62%*\n\t.\n```\nclass Solution {\n    public int nthSuperUglyNumber(int n, int[] primes) {\n          int[] pointers = new int[primes.length];\n          int[] dp = new int[n];\n          dp[0] = 1;\n          for(int i = 0; i < pointers.length; i++)\n              pointers[i] = 0;\n\n          for(int i = 1; i < dp.length; i++) {\n              int min = Integer.MAX_VALUE;\n              for(int j = 0; j < pointers.length; j++)\n                  min = Integer.min(min, primes[j] * dp[pointers[j]]);\n\n              dp[i] = min;\n              for(int j = 0; j < pointers.length; j++) {\n                  if(dp[i] == primes[j] * dp[pointers[j]])\n                      pointers[j]++;\n              }\n          }\n          return dp[n-1];\n    }\n}\n```\n.\n**2. By using Priority Queue**\n\tTime Complexity - O(N* log(K))\n\tSpace Complexity - O(N + K)\n\tRuntime - 445ms - Faster than 36.13%\n\t.\n```\nclass Solution {\n    public class Pair implements Comparable<Pair>{\n        int primes;\n        int pointers;\n        int value;\n        \n        Pair(int primes, int pointers, int value){\n        \tthis.primes = primes;\n        \tthis.pointers = pointers;\n        \tthis.value = value;\n        }\n        \n        public int compareTo(Pair o) {\n        \treturn this.value - o.value;\n        }\n    }\n    \n    public int nthSuperUglyNumber(int n, int[] primes) {\n          int[] dp = new int[n];\n          dp[0] = 1;\n\n          PriorityQueue<Pair> pq = new PriorityQueue<>();\n          for(int i = 0; i < primes.length; i++)\n              pq.add(new Pair(primes[i], 0, primes[i] * dp[0]));\n\n          for(int i = 1; i < dp.length; i++) {\n              Pair rem = pq.remove();\n              dp[i] = rem.value;\n              pq.add(new Pair(rem.primes, rem.pointers + 1, rem.primes * dp[rem.pointers]));\n              while(pq.peek().value == rem.value) {\n                  Pair nrem = pq.remove();\n                  pq.add(new Pair(nrem.primes, nrem.pointers + 1, nrem.primes * dp[nrem.pointers]));\n              }\n          }\n          return dp[n-1];\n    }\n}\n```
['Java']