C++ code beats 98%


  • 0
    Y
    class Solution {
    public:
        int min(int *factor,int len){
            int minNum=factor[0];
            for(int i=1;i<len;i++)
                if(factor[i]<minNum)
                   minNum=factor[i];
            return minNum;
        }
        int nthSuperUglyNumber(int n, vector<int>& primes) {
            int len=primes.size();
            int *index=new int[len];
            int *factor=new int[len];
            int *ugly=new int[n];
            ugly[0]=1;
            int minNum;
            for(int i=0;i<len;i++)
            {
                factor[i]=primes[i];
                index[i]=0;
            }
            for(int i=1;i<n;i++)
            {
                minNum=min(factor,len);
                ugly[i]=minNum;
                for(int i=0;i<len;i++)
                {
                    if(minNum==factor[i])
                    {
                        factor[i]=primes[i]*ugly[++index[i]];
                    }
                }
            }
            return ugly[n-1];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.