当前位置:网站首页>Leetcode skimming -- bit by bit record 023
Leetcode skimming -- bit by bit record 023
2022-07-23 10:44:00 【Robust least squares support vector machine】
23. The finger of the sword Offer 49. Ugly number
requirement
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 .
Ideas
Recurrence properties of ugly numbers : Ugly numbers contain only factors 2, 3, 5 , So there is “ Ugly number = Some clown number × Some factor ”
Dynamic programming :
- State definition : Set up a dynamic programming list dp,dp[i] On behalf of the i + 1 Ugly number
- Transfer equation :
When indexing a, b, c When the following conditions are met , dp[i] Is the minimum of three cases ;
Calculation per round dp[i] after , Need to update index a, b, c Value , Make it always meet the conditions of the equation . Implementation method : Judge independently dp[i] and dp[a]×2 , dp[b]×3 , dp[c]×5 The size of the relationship , If equal, the corresponding index a , b, c Add 1 - The initial state : dp[0] = 1 , That is, the first ugly number is 1
- Return value : dp[n-1] , That is, return to the second page n Ugly number
Problem solving
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
int nthUglyNumber(int n) {
int a = 0, b = 0, c = 0;
vector<int> dp(n + 1);
dp[0] = 1; // The initial state
for (int i = 1; i < n; i++) {
int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5;
dp[i] = min(min(n2, n3), n5); // Minimum value of three cases
if (dp[i] == n2)
a++;
if (dp[i] == n3)
b++;
if (dp[i] == n5)
c++;
}
return dp[n - 1]; // Back to page n Ugly number
}
};
void test01()
{
Solution solution;
int n = 10;
cout << solution.nthUglyNumber(n) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

I hope this article can help you , If there is anything wrong with the above , Welcome to correct
Sharing determines the height , Learn to widen the gap
边栏推荐
- Introduction to partition operators, broadcast variables and accumulators of 32 spark
- 数据湖:从数据仓库看数据湖
- [Delphi] a simple method to make the installation icon of the control panel (translation)
- New file / filter / folder in VS
- Kingbasees SQL language reference manual of Jincang database (8. Function (8))
- How to query data differences between two isomorphic tables of massive data
- Add trust list
- [qt5.12] qt5.12 installation tutorial
- MapReduce进阶
- selenium JD爬虫
猜你喜欢

SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION

Sequence model (III) - sequence model and attention mechanism

Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library

JMeter record the BeanShell written into excel instance caused by an automatic data generation

Rapid SQL all platforms high performance SQL code

第四篇章:运行时数据区——共享空间

Kubernetes技术与架构(六)

How to protect the copyright of NFT digital collections?

SQLZOO——SELECT from WORLD Tutorial

The safe distance between you and personal information leakage may be decided by a laptop!
随机推荐
Flutter 运行flutter pub get 报错“客户端没有所需特权“
网线水晶头接法图解8根顺序
How to delete an item in sonar
Jmeter-记一次自动化造数引发的BeanShell写入excel实例
Redis pseudo cluster one click deployment script - pro test available
Visual Studio 2022有趣又强大的智能辅助编码
DPDK 交叉编译基本流程
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
注册树模式
kex_exchange_identification: read: Connection reset by peer 不完美解决办法(之一)
Chapter 3 Standard Input
MySql 数据库表名命名规则---方便自动转换工具转换
Hololens third perspective development [nanny level tutorial] [stepping on the pit record]
Rapid SQL all platforms high performance SQL code
PyQt5_QListWidget分页多选控件
Flask学习笔记
解决servlet中post请求和get请求中文乱码现象
kex_ exchange_ Identification: read: connection reset by peer imperfect solution (one)
8 < tag dynamic programming and LCS problems > lt.300. Longest increasing subsequence + lt.674. Longest continuous increasing sequence
写驱动程序的时候warning LNK4210报错