教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode313. 超级丑数 | Super Ugly Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10260454.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Example:

primes
[2,7,13,19]
[1,2,4,7,8,13,14,16,19,26,28,32] 
primes
[2,7,13,19]

Note:

  • 1 is a super ugly number for any given primes.
  • The given numbers in primes are in ascending order.
  • 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
  • The nth super ugly number is guaranteed to fit in a 32-bit signed integer.

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

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

示例:

primes
[2,7,13,19]

说明:

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

116 ms

复制代码
 1 class Solution {
 2     func nthSuperUglyNumber(_ n: Int, _ primes: [Int]) -> Int {
 3         let count = primes.count
 4         
 5         var index = Array(repeatElement(0, count: count))
 6         var value = primes
 7         var temp = 0
 8 
 9         var ugly = [1]
10         for _ in 0..<n - 1 {
11             temp = Int.max
12             for j in 0..<count {
13                 temp = min(temp, value[j])
14             }
15             ugly.append(temp)
16             for j in 0..<count {
17                 if temp == value[j] {
18                     index[j] += 1
19                     value[j] = ugly[index[j]] * primes[j]
20                 }
21             }
22         }
23         return ugly[n - 1]
24     }
25 }
复制代码

556ms

复制代码
 1 class Solution {
 2     func nthSuperUglyNumber(_ n: Int, _ primes: [Int]) -> Int {
 3         var res = [1] 
 4         var c = Array(repeating: 0, count: primes.count)
 5         
 6         for _ in 0 ..< n-1 {
 7             var comp = [Int]()
 8             for i in 0 ..< primes.count {
 9                 comp.append(res[c[i]] * primes[i])
10             }
11             
12             let minP = comp.min()!
13             res.append(minP)
14             
15             for j in 0 ..< comp.count where comp[j] == minP {
16                 c[j] += 1
17             }
18         }
19         
20         return res.last!
21     }
22 }
复制代码

 

posted @   为敢技术  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°