O(NK) Solution with further optimisation to O(NlogK) by MIN HEAP
O(NK) Solution :\n\n\nclass Solution {\npublic:\n    int nthSuperUglyNumber(int n, vector<int>& primes) {\n        int dp[n + 1]; dp[1] = 1;\n        \n        
O(NK) Solution :\n\n```\nclass Solution {\npublic:\n    int nthSuperUglyNumber(int n, vector<int>& primes) {\n        int dp[n + 1]; dp[1] = 1;\n        \n        vector<int> pointers(primes.size(), 1);\n        \n        for (int i = 2; i<=n; i++) {\n            int mn = INT_MAX;\n            for (int j = 0; j<primes.size(); j++) {\n                mn = min(mn, primes[j] * dp[pointers[j]]);\n            }\n            \n            dp[i] = mn;\n            \n            for (int j = 0; j<primes.size(); j++) {\n               if ( mn == primes[j] * dp[pointers[j]]) pointers[j]++;\n            }\n        }\n        \n        return dp[n];\n    }\n};\n````\nO(NlogK) Solution : Use min heap for finding minimum \n```\nclass Solution {\npublic:\n    int nthSuperUglyNumber(int n, vector<int>& primes) {\n        int dp[n + 1]; dp[1] = 1;\n        \n\t\t// pq contains vector of size three where first element is product (prime * dp[pointer] ), \n\t\t// second is prime itself and third is the index of pointer.\n\t\t\n        priority_queue< vector<int>, vector<vector<int>>, greater<> > pq;\n        for (int i = 0; i<primes.size(); i++)  pq.push({primes[i], primes[i], 1});\n\n        for (int i = 2; i<=n; i++) {\n            int mn = pq.top()[0];\n\n            dp[i] = mn;\n\t\t\t\n\t\t\t// increase pointers corresponding to all minimums, so loop is required\n            while(!pq.empty() && pq.top()[0] == mn) {\n                vector<int> temp = pq.top(); pq.pop();\n                temp[2]++; temp[0] = temp[1] * dp[temp[2]];\n                pq.push(temp);\n            }\n        }\n        \n        return dp[n];\n    }\n};\n
['Dynamic Programming', 'C', 'Heap (Priority Queue)']