O(n*k) Java and C  solution
I would just like to thank lixx2100 for point out a very good point that after we find the min we can just update all necessary pointers in one more pass. \n\nC
I would just like to thank lixx2100 for point out a very good point that after we find the min we can just update all necessary pointers in one more pass. \n\nC code:\n\n    #define MIN(a,b)  (a) ^ (((b) ^ (a)) & -((b) < (a)))\n    \n    int nthSuperUglyNumber(int n, int* primes, int primesSize) {\n        int* indexes = (int*) calloc(primesSize, sizeof(int));\n        int* uglyNums = (int*) malloc(sizeof(int)*n);\n        \n        *uglyNums = 1;\n        \n        for(int i = 1; i < n; ++i){\n            int min = 0x7FFFFFFF;\n            \n            for(int j = 0; j < primesSize; ++j)\n                min = MIN(min, uglyNums[indexes[j]]*primes[j]);\n                \n            uglyNums[i] = min;\n            for(int j = 0; j < primesSize; ++j)\n                if(uglyNums[indexes[j]]*primes[j] == min)\n                    ++indexes[j];\n        }\n        return uglyNums[n-1];\n    }\n\n\nJava Code:\n\n    public class Solution {\n        public int nthSuperUglyNumber(int n, int[] primes) {\n    \n            int[] indexs = new int[primes.length];\n            int[] uglyNums = new int[n];\n            uglyNums[0] = 1;\n            \n            for(int i = 1; i < n; ++i){\n                int min = uglyNums[indexs[0]]*primes[0];\n                \n                for(int j = 1; j < primes.length; ++j){\n                    int temp = uglyNums[indexs[j]]*primes[j];\n                    \n                    if(min > temp & temp > 0)\n                        min = temp;\n                }\n                uglyNums[i] = min;\n                for(int j = 0; j < primes.length; ++j){\n                    if(uglyNums[indexs[j]]*primes[j] == min)\n                        ++indexs[j];\n                }\n            }\n            return uglyNums[n-1];\n        }\n    }
['Dynamic Programming', 'Java']