本文主要是介绍LeetCode *** 313. Super Ugly Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes
of size k
. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
is the sequence of the first 12 super ugly numbers given primes
= [2, 7, 13, 19]
of size 4.
Note:
(1) 1
is a super ugly number for any given primes
.
(2) The given numbers in primes
are in ascending order.
(3) 0 < k
≤ 100, 0 < n
≤ 106, 0 < primes[i]
< 1000.
class Solution {
public:int nthSuperUglyNumber(int n, vector<int>& primes) {vector<int> res(n,INT_MAX),index(primes.size(),0);res[0]=1;int size=primes.size();for(int i=1;i<n;++i){for(int j=0;j<size;++j)res[i]=min(res[i],res[index[j]]*primes[j]);for(int j=0;j<size;++j)index[j]+=(res[i]==res[index[j]]*primes[j]);}return res[n-1];}
};
这篇关于LeetCode *** 313. Super Ugly Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!