Java Heap Solution with comments Time Complexity = O(logk * N)
The Idea is any  prime number array we have to use factor of those prime number i.e from [2,3,5] then 2X1, 3X1,  2X2, 5X1, 2X3...\nWe keep adding all these prim
**The Idea is any  prime number array we have to use factor of those prime number i.e from [2,3,5] then 2X1, 3X1,  2X2, 5X1, 2X3...\nWe keep adding all these prime numbers into heap and keep removing and storing that primes numbers factors into array and keep updating for the prime number next factor.\nWe need to keep in ind when 2X3==6 and 3X2 = 6 then we will be already able to store 6 as the ugly number in the array but then we will needed both of 2 and 3 prime number\'s being updated for their index[idx] , hence we need a loop for that while .**\n\nTime Complexity :- n = total of inputs of the ugly number k will be the same logk for adding log( k) and removing the same value max till log(k) from heap, so O(N*logk)\nSpace Complexity :- is N for ugly numbers and k for primes\' array so O(N + k)\n\n```\nclass Solution {\n    \n   class ugly implements Comparable<ugly>{\n       \n       int val; //this is for the value\n       int idx;  //idx which will be used for storing next number for primes[i]\n       int set; //this is used for the primes[i] number which will be multiplied with the values\n       \n       public ugly(int val, int idx, int set){\n           this.val = val;\n           this.idx = idx;\n           this.set = set;\n       }\n       \n       public int compareTo(ugly p){\n          \n           return this.val-p.val;\n       }\n   }\n        \n    public int nthSuperUglyNumber(int n, int[] primes) {\n        \n        PriorityQueue<ugly> pq = new PriorityQueue<>();\n        int[] arrugly = new int[n];\n        \n        for(int i = 0 ; i <primes.length; i++){\n            pq.add(new ugly(primes[i],1,primes[i]));\n        }\n        \n        arrugly[0] = 1;\n        \n        for(int i = 1; i<n; i++){\n//here let we have gone till 1,2, this time i = 3 we get min values i.e. 3 out of heap and store arrugly[2] = 3\n            \n            arrugly[i] = pq.peek().val;\n //taking the while loop because at a time i.e 2 factor = 6 and 3 factor = 6 so we will update respecitively of 2 and 3 factors\n            \n            while(pq.peek().val==arrugly[i]){\n            ugly rem = pq.remove();\n            pq.add(new ugly(arrugly[rem.idx]*rem.set, rem.idx+1, rem.set));    \n//now arrugly[1]=2 and 2* rem.set(==3) = 6 and idx+1=2 for next iteration like next will be arrugly[2] = 3 so 3*rem.set(==3) = 9 and so on..            \n            \n            }\n            \n        }\n        \n        return arrugly[n-1];\n    }\n}\n```
['Heap (Priority Queue)', 'Java']