zoukankan      html  css  js  c++  java
  • 313. 超级丑数

    编写一段程序来查找第 n 个超级丑数。

    超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。

    示例:

    输入: n = 12, primes = [2,7,13,19]
    输出: 32
    解释: 给定长度为 4 的质数列表 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
    说明:

    1 是任何给定 primes 的超级丑数。
     给定 primes 中的数字以升序排列。
    0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000 。
    第 n 个超级丑数确保在 32 位有符整数范围内。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/super-ugly-number

    最小堆

    class Solution {
        public int nthSuperUglyNumber(int n, int[] primes) {
            PriorityQueue<Long>q=new PriorityQueue<>();
            long res=1;
            for(int i=1;i<n;i++){
                for(int prime:primes){
                    q.add(prime*res);
                }
                res=q.poll();
                while(!q.isEmpty()&&res==q.peek())
                    q.poll();
            }
            return (int)res;
        }
    }
  • 相关阅读:
    正则表达式--hdu2206ip匹配
    win7查看隐藏分区
    我购买byd的几点逻辑
    html5笔记
    机器学习
    Popular Cows
    武大OJ 574. K-th smallest
    武大OJ 622. Symmetrical
    [HAOI2011]防线修建
    1185: [HNOI2007]最小矩形覆盖
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13770516.html
Copyright © 2011-2022 走看看