当前位置:网站首页>49. ugly number
49. ugly number
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 49. Ugly number
Ideas : Dynamic programming
Let the known length be n Sequence of ugly numbers x 1 , x 2 , x 3 , ⋯ , x n x_1,x_2,x_3,\cdots,x_n x1,x2,x3,⋯,xn, Please n+1 Ugly number , According to the recursive nature , Ugly number x n + 1 x_{n+1} xn+1 It can only be for :
f ( x ) = { x a × 2 a ∈ [ 1 , n ] x b × 3 b ∈ [ 1 , n ] x c × 5 c ∈ [ 1 , n ] f(x)=\begin{cases} x_a\times2 & {a\in[1,n]}\\ x_b\times3 & {b\in[1,n]}\\ x_c\times5 & {c\in[1,n]} \end{cases} f(x)=⎩⎪⎨⎪⎧xa×2xb×3xc×5a∈[1,n]b∈[1,n]c∈[1,n]
The recurrence formula of ugly numbers is :
x n + 1 = m i n ( x a × 2 , x b × 3 , x c × 5 ) x_{n+1}=min(x_a\times2,x_b\times3,x_c\times5) xn+1=min(xa×2,xb×3,xc×5)
because x n + 1 x_{n+1} xn+1 Is the closest to x n x_n xn Ugly number , So index a, b, c The following conditions are met :
{ x a × 2 > x n ≥ x a − 1 × 2 namely x a by The first individual ride With 2 after Big On x n Of ugly Count x b × 3 > x n ≥ x b − 1 × 3 namely x b by The first individual ride With 3 after Big On x n Of ugly Count x c × 5 > x n ≥ x c − 1 × 5 namely x c by The first individual ride With 5 after Big On x n Of ugly Count \begin{cases} x_a\times2>x_n\ge x_{a-1}\times2 & namely x_a First times 2 Later is greater than x_n Ugly number \\ x_b\times3>x_n\ge x_{b-1}\times3 & namely x_b First times 3 Later is greater than x_n Ugly number \\ x_c\times5>x_n\ge x_{c-1}\times5 & namely x_c First times 5 Later is greater than x_n Ugly number \end{cases} ⎩⎪⎨⎪⎧xa×2>xn≥xa−1×2xb×3>xn≥xb−1×3xc×5>xn≥xc−1×5 namely xa by The first individual ride With 2 after Big On xn Of ugly Count namely xb by The first individual ride With 3 after Big On xn Of ugly Count namely xc by The first individual ride With 5 after Big On xn Of ugly Count
therefore , Set the pointer a,b,c Point to the first ugly number , The next ugly number is obtained by the circular recurrence formula , And each round will correspond to the pointer +1
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> dp(n);
dp[0]=1;
int a=0,b=0,c=0;
for(int i=1;i<n;++i){
int na=dp[a]*2;
int nb=dp[b]*3;
int nc=dp[c]*5;
dp[i]=min(na,min(nb,nc));
if(dp[i]==na)++a;
if(dp[i]==nb)++b;
if(dp[i]==nc)++c;
}
return dp[n-1];
}
};
Time complexity O(n)
Spatial complexity O(n)
Ideas : Heap + Hash table de duplication
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> factors={2,3,5};
unordered_set<long> seen;
priority_queue<long,vector<long>,greater<long>> heap;
seen.insert(1L);
heap.push(1L);
int ugly=0;
for(int i=0;i<n;++i){
long curr=heap.top();
heap.pop();
ugly=int(curr);
for(int factor:factors){
long next=curr*factor;
if(!seen.count(next)){
seen.insert(next);
heap.push(next);
}
}
}
return ugly;
}
};
Time complexity O(n*logn)
Spatial complexity O(n)
边栏推荐
- Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
- Ten trends of Internet Security in 2022 industry released
- The combined application of TOPSIS and fuzzy borde (taking the second Dawan District cup and the national championship as examples, it may cause misunderstanding, and the Dawan District cup will be up
- Servlet core technology
- One dragon and one knight accompanying programmers are 36 years old
- ShanMeng and Beijing Adoption Day start NFT digital collection public offering
- Quickly get PCA (principal component analysis) (principle code case)
- 57 - II. Continuous positive sequence with sum s
- Image processing 13- calculation of integral diagram
- How to clear floating, and how does it work?
猜你喜欢

Thingsboard create RCP widget

According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"
![[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)](/img/60/baebffb2024ddf5f2cb070f222b257.jpg)
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)

When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path

Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind

It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application

Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)
![[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e](/img/2e/97310ec36aeb1fc1e9c82361141a36.jpg)
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e

ShanMeng and Beijing Adoption Day start NFT digital collection public offering

How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2
随机推荐
Platform of ASoC framework driven by alsa
File contains (regardless of suffix) Apache log remote file contains PHP encapsulated pseudo protocol:
JS controls the display and hiding of tags through class
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
Asp. Net core EF value conversion
Musk promotes the development of fascinating new products partners remind important questions
Servlet core
How to deploy dolphin scheduler 1.3.1 on cdh5
National land use data of 30m precision secondary classification
Kwai opens a milestone activity for fans to record every achievement moment for creators
How to count the total length of roads in the region and draw data histogram
Applet pull-down load refresh onreachbottom
Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)
asp. Net core theme Middleware
Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
Walking "daily question" and "DP"
Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
One dragon and one knight accompanying programmers are 36 years old
Some problems of Qinglong panel
LabVIEW關於TDMS和Binary存儲速度