leetcode:数学: Super Ugly Number(313)

原创 2016年08月30日 15:55:04

Write a program to find the nth 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(1, 1), idx(primes.size(), 0);
        while (res.size() < n) {
            vector<int> tmp;
            int mn = INT_MAX;
            for (int i = 0; i < primes.size(); ++i) {
                tmp.push_back(res[idx[i]] * primes[i]);
            }
            for (int i = 0; i < primes.size(); ++i) {
                mn = min(mn, tmp[i]);
            }
            for (int i = 0; i < primes.size(); ++i) {
                if (mn == tmp[i]) ++idx[i];
            }
            res.push_back(mn);
        }
        return res.back();
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LEETCODE】313-Super Ugly Number [Python]

yield,map,heapq.merge,next

[Leetcode] 313. Super Ugly Number 解题报告

题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbe...

leetcode 313. Super Ugly Number

Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose a...

LeetCode-313. Super Ugly Number (JAVA)超级丑数

LeetCode-313. Super Ugly Number (JAVA)超级丑数

Leetcode 313. Super Ugly Number

leetcode超级丑数思路及代码,时间复杂度O(n*k)
  • xindoo
  • xindoo
  • 2016年06月19日 19:53
  • 293

313. Super Ugly Number

找出第n个super ugly number; prime factor, priority queue;

<LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)

263. Ugly Number My Submissions Question Total Accepted: 33755 Total Submissions: 96873 Difficul...

Leetcode Super Ugly Number

题目写一个程序查找第n个super ugly number. 一个super ugly number 是所有质数由给定列表中的质数构成的正整数。比如,对给定的素数列表:primes = [2, 7,...

Leetcode之super ugly number

Leetcode之super ugly number,java优先队列,python heapq

LeetCode:Super Ugly Number系列

1、Ugly Number IIWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose...
  • BestZem
  • BestZem
  • 2016年07月13日 21:44
  • 356
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:数学: Super Ugly Number(313)
举报原因:
原因补充:

(最多只允许输入30个字)