[leetcode] Super Ugly Number

用时24ms。

天呐好懒不想写解释先存个草稿放在这里吧……等我找只黄鸭先

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        int ps=primes.size();
        int answer[n], possible_current_answer[ps];
        vector<int> t(ps,0);
        answer[0]=1;
        for (int k=1;k<n;k++) {
            //answer[k]=max{answer[t[i]]*primes[i], i=0:ps-1.}
            for (int i=0;i<ps;i++)
                possible_current_answer[i]=answer[t[i]]*primes[i];
            answer[k]=*min_element(possible_current_answer, possible_current_answer+ps);
            for (int i=0;i<ps;i++) {
                if (answer[k]==possible_current_answer[i])
                    ++t[i];
            }
        }
        return answer[n-1];
    }
};

猜你喜欢

转载自www.cnblogs.com/RDaneelOlivaw/p/10346989.html
今日推荐