【leetcode】313. 超级丑数

#define INT_MAX 2147483647
int nthSuperUglyNumber(int n, int* primes, int primesSize){
    if(n == 1) return 1;
    int *res = (int *)calloc(n, sizeof(int));
    int rsize = 0;
    int *pids = (int *)calloc(primesSize, sizeof(int));
    res[rsize++] = 1;
    while(rsize < n){
        int min = INT_MAX;
        int id = 0;
        for(int i = 0; i < primesSize; i++){
            int tmin = primes[i] * res[ pids[i] ];
            if(tmin < min)
            {
                min = tmin;
                id = i;
            }
            // 去除重复
            else if(tmin == min)
            {
                pids[i]++;
            }
        }
        res[rsize++] = min;
        pids[id]++;
    }
    return res[n - 1];
}

 

posted @ 2021-01-20 14:52  温暖了寂寞  阅读(75)  评论(0编辑  收藏  举报