当前位置:网站首页>Sword finger offer 49. ugly number
Sword finger offer 49. ugly number
2022-07-24 19:27:00 【nsq1101】
subject
We will include only qualitative factors 2、3 and 5 The number of is called ugly (Ugly Number). Seek the order from small to large n Ugly number .
Example :
Input : n = 10
Output : 12
explain : 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 Before 10 Ugly number .
explain :
1 It's ugly .
n No more than 1690.
Ideas
- Dynamic programming
- 2,3,5 Multiple in , The smallest one now
Program
class Solution:
def nthUglyNumber(self, n: int) -> int:
dp = [1] * n
a,b,c=0,0,0
for i in range(1,n):
n2 = dp[a] * 2
n3 = dp[b] * 3
n5 = dp[c] * 5
dp[i] = min(n2,n3,n5)
if dp[i] ==n2: a = a+ 1
if dp[i] ==n3: b = b+ 1
if dp[i] ==n5: c = c+ 1
return dp[-1]
边栏推荐
- Cesium uses czml to implement dynamic routes
- 湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭
- Colon sorting code implementation
- Original reverse compensation and size end
- 【JVM学习03】类加载与字节码技术
- OpenGL learning (III) glut two-dimensional image rendering
- Leetcode402 remove K digits
- asp. Net core, C # summary about path
- 思源笔记 v2.1.2 同步问题
- Tencent Browser service TBS usage
猜你喜欢
![[JVM learning 04] JMM memory model](/img/8c/0f76be122556301a5af140e34b55f4.png)
[JVM learning 04] JMM memory model

Pam4 popular science

C # shelling tool for code encryption protection

Sequences, time series and prediction in tessorflow quizs on coursera (II)

Implement a proxy pool from 0

OpenGL learning (IV) glut 3D image rendering

Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)

MySQL1

Meshlab&PCL ISS关键点

OpenGL learning (III) glut two-dimensional image rendering
随机推荐
Reading notes of XXL job source code
PostgreSQL weekly news - July 13, 2022
MySQL1
Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
Clion configuring WSL tool chain
Rotation matrix derivation process
文献阅读:GoPose 3D Human Pose Estimation Using WiFi
信道状态信息(CSI)共轭相乘去噪法
Tencent Browser service TBS usage
Nacos introduction and console service installation
JVM method call
Meshlab&PCL ISS关键点
Channel state information (CSI) conjugate multiplication denoising method
Interceptors and filters
[JVM learning 03] class loading and bytecode Technology
day 3
asp. Net core, C # summary about path
Compressed string
About core files