Leetcode 313: Super Ugly Number (超级丑数)

这篇具有很好参考价值的文章主要介绍了Leetcode 313: Super Ugly Number (超级丑数)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. Super Ugly Number
    Medium
    A super ugly number is a positive integer whose prime factors are in the array primes.
    Given an integer n and an array of integers primes, return the nth super ugly number.
    The nth super ugly number is guaranteed to fit in a 32-bit signed integer.

Example 1:

Input: n = 12, primes = [2,7,13,19]
Output: 32
Explanation: [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].
Example 2:

Input: n = 1, primes = [2,3,5]
Output: 1
Explanation: 1 has no prime factors, therefore all of its prime factors are in the array primes = [2,3,5].

Constraints:

1 <= n <= 105
1 <= primes.length <= 100
2 <= primes[i] <= 1000
primes[i] is guaranteed to be a prime number.
All the values of primes are unique and sorted in ascending order.

解法1:用Heap做。注意去重可以用topNode.product != uglys[index]。文章来源地址https://www.toymoban.com/news/detail-725802.html

struct Node {
    int prime;
    int index;
    long long product;
    Node(int pri, int id, long long pro) : prime(pri), index(id), product(pro) {}
    bool operator < (const Node & node) const {
        return product >= node.product;
    }
};

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        int primesCount = primes.size();
        vector<long long> uglys(n + 1, 0);
        vector<int> indexes(primesCount, 1);
        priority_queue<Node> minHeap;
        uglys[1] = 1;
        int index = 1;
        for (int i = 0; i < primesCount; i++) {
            minHeap.push(Node(primes[i], 1, primes[i])); //1 * primes[i] = primes[i]
        }

        while (index <= n) {
            int minV = INT_MAX;
            Node topNode = minHeap.top();
            minHeap.pop();
            if (topNode.product != uglys[index]) {
                if (index < n) {
                    uglys[++index] = topNode.product;
                }
                else break;
            }
            minHeap.push(Node(topNode.prime, topNode.index + 1, uglys[topNode.index + 1] * topNode.prime));
        }
        return (int)uglys[n];
    }
};

到了这里,关于Leetcode 313: Super Ugly Number (超级丑数)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(36)
  • LeetCode287. Find the Duplicate Number

    Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space. Example 1: Input: nums = [1,3,4,2,2] Output: 2 Example 2: Input: nums = [3,1,3,4,

    2024年01月20日
    浏览(43)
  • LeetCode171. Excel Sheet Column Number

    Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. For example: A - 1 B - 2 C - 3 … Z - 26 AA - 27 AB - 28 … Example 1: Input: columnTitle = “A” Output: 1 Example 2: Input: columnTitle = “AB” Output: 28 Example 3: Input: columnTitle = “ZY” Output: 701 Constraints:

    2024年02月19日
    浏览(40)
  • LeetCode 933. Number of Recent Calls

    ou have a  RecentCounter  class which counts the number of recent requests within a certain time frame. Implement the  RecentCounter  class: RecentCounter()  Initializes the counter with zero recent requests. int ping(int t)  Adds a new request at time  t , where  t  represents some time in milliseconds, and returns the number of requests that has h

    2024年02月08日
    浏览(47)
  • LeetCode405. Convert a Number to Hexadecimal

    Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used. All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself. Note: You are not allowed to use any built-in library method to di

    2024年02月19日
    浏览(27)
  • LeetCode //C - 933. Number of Recent Calls

    You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the pas

    2024年01月23日
    浏览(49)
  • LeetCode --- 1903. Largest Odd Number in String 解题报告

    You are given a string  num , representing a large integer. Return  the  largest-valued odd  integer (as a string) that is a  non-empty substring  of  num , or an empty string  \\\"\\\"  if no odd integer exists . A  substring  is a contiguous sequence of characters within a string. Example 1: Example 2: Example 3:

    2024年02月10日
    浏览(35)
  • LeetCode452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons. Arrows can be shot up directly vertically (in

    2024年02月04日
    浏览(44)
  • LeetCode 1921. Eliminate Maximum Number of Monsters【贪心,计数排序】1527

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(52)
  • LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [ x s t a r t , x e n d x_{start}, x_{end} x s t a r t ​ , x e n d ​ ] denotes a balloon whose horizontal diameter stretches between x s t a r t x_{start} x s t a r t ​ and x e n d x_{end} x

    2024年02月12日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包