154 - 264 丑数2
题目
编写一个程序,找出第 n 个丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。
示例:
输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。
说明:
1 是丑数。
n 不超过1690。
解答
能想到的方法,就是不断除2,3,5,如果除光了就是丑数了。
https://leetcode-cn.com/problems/ugly-number-ii/solution/dui-he-dong-tai-gui-hua-by-powcai/
小顶堆
维护一个小顶堆,根就是最小值。因此每次把最小值拿出来,乘以2,3,5,就是丑数的可能性。
循环n遍拿的最小值,就是答案了
class Solution:
def nthUglyNumber(self, n: int) -> int:
import heapq
heap = [1]
heapq.heapify(heap)
res = 0
for _ in range(n):
res = heapq.heappop(heap)
while heap and res == heap[0]:
res = heapq.heappop(heap)
a, b, c = res*2, res*3, res*5
for t in [a, b, c]:
heapq.heappush(heap, t)
return resRuntime: 244 ms, faster than 28.36% of Python3 online submissions for Ugly Number II.
Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Ugly Number II.
动态规划
dp[i] = min(2 * dp[i2], 3 * dp[i3], 5 * dp[i5])
丑数是,2、3、5的倍数。即丑数=n个2*2+m个3*3+k个5*5。比如dp[1]= 2 = 1*2+0*3+0*5
我们要维护一个数组,里面是丑数从小到大排列,第n个值就是答案。
也就是说,在数组里的每个数,在未来数组里面都会被2、3、5乘一遍
于是我们就维护三个指针,分别指下一个还没被2乘、3乘、5乘的数。然后取最小值记录在dp里面
Runtime: 156 ms, faster than 73.55% of Python3 online submissions for Ugly Number II.
Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Ugly Number II.
Runtime: 68 ms, faster than 75.68% of JavaScript online submissions for Ugly Number II.
Memory Usage: 36.4 MB, less than 100.00% of JavaScript online submissions for Ugly Number II.
Runtime: 0 ms, faster than 100.00% of Go online submissions for Ugly Number II.
Memory Usage: 4.1 MB, less than 42.86% of Go online submissions for Ugly Number II.
Last updated
Was this helpful?