dp 编程
Problem: You are given N. You have to find Nth ugly number. 1st ugly number is 1.
问题:给您N。 您必须找到第 N 个丑数。 1 件难看数为1。
Constraints: 1 <= N <= 10000
限制条件: 1 <= N <= 10000
Examples:
例子:
Input:
10
Output:
10th ugly number is 12
Input:
724
Output:
724th ugly number is 7085880
Explanation of the problem:
问题说明:
Ugly numbers are those number whose only prime factors are 2, 3, 5 (except 1). 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ..., So our 10th ugly number is 12.
丑数是仅素数为2、3、5 (1除外)的那些数。 1,2,3,4,5,6,8,9,10,12,... ,所以我们的第十个丑数是12 。
Solution:
解:
The basic and the brute force approach for solving this problem is to iterate over all the numbers and check whether it is ugly or not then incrementing the counter but this approach will be highly inefficient to solve. Therefore, we will discuss the Dynamic Programming approach. Now we will take a dp matrix in which ith cell represent ith ugly number. Then we will find the answer to ugly number using this dp relation.
解决此问题的基本方法和蛮力方法是遍历所有数字并检查其是否丑陋,然后递增计数器,但是这种方法解决效率极低。 因此,我们将讨论动态编程方法。 现在我们将使用一个dp矩阵,其中第 i 个单元代表第一个丑数。 然后,我们将使用此dp关系找到丑数的答案。
Ugly number(i) = minimum of (2*ugly number(f2), 3*ugly number(f3), 5*ugly number(f5))
丑数(i)=(2 *丑数(f2),3 *丑数(f3),5 *丑数(f5))的最小值
Where, f2, f3, f5 are counters for 2, 3, 5 respectively which stores the index for f2th ugly number, f3th ugly number, f5th ugly number.
其中,F2,F3,F5是为2,3,5分别计数器,其存储该指数为f2的第难看数,F3 第难看数,F5 第难看数。
Then we will store our answer.
然后,我们将存储我们的答案。
Algorithm:
算法:
Initializing f2, f3, f5 to maintain f2th ugly number, f3th ugly number and f5th ugly number to 1.
初始化F2,F3,F5,以保持第 F2难看数,F3 第难看数目以及f5 第难看数为1。
Creating a DP matrix in which ith cell represent ith ugly number.
创建一个DP矩阵,其中第一个单元格代表第一个数字。
Start filling the dp matrix from i = 2.
从i = 2开始填充dp矩阵。
Ugly number(i) = minimum of (2*ugly number(f2), 3*ugly number(f3), 5*ugly number(f5)).
丑数(i)=(2 *丑数(f2),3 *丑数(f3),5 *丑数(f5))的最小值 。
Finding the answer using above relation and storing it at ith index.
使用上述关系找到答案并将其存储在第 i 个索引处。
Now, the time complexity of the above code is O(N).
现在,以上代码的时间复杂度为O(N)。
Program:
程序:
#include <iostream>
using namespace std;
long long int uglyNumber(int n){
// f2 maintains the index of the ugly number
// for multiplication by 2
int f2 = 1;
// f3 maintains the index of the ugly number
// for multiplication by 3
int f3 = 1;
// f5 maintains the index of the ugly number
// for multiplication by 5
int f5 = 1;
long long int dp[n + 1] = {0};
// first ugly number is 1
dp[1] = 1;
// filling the dp matrix from i = 2
for(int i = 2;i<=n;i++){
// calculating the next number from previous ugly numbers
long long int ans_f_2 = dp[f2] * 2;
long long int ans_f_3 = dp[f3] * 3;
long long int ans_f_5 = dp[f5] * 5;
// finding minimum of all the answer
long long int final_ans = min(ans_f_2, min(ans_f_3, ans_f_5));
// storing the ans
dp[i] = final_ans;
// if our final_ans is ans_f_2, ans_f_3, ans_f_5
// then increment their counters i.e f2, f3, f5
if(final_ans == ans_f_2){
f2++;
}
if(final_ans == ans_f_3){
f3++;
}
if(final_ans == ans_f_5){
f5++;
}
}
return dp[n];
}
// driver program to check the code
int main() {
int n;
cin >> n;
cout << n << endl;
cout<< n << "th ugly number is " <<uglyNumber(n) << endl;
return 0;
}
Output
输出量
10
10
10th ugly number is 12
翻译自: https://www.includehelp.com/algorithms/finding-ugly-number-using-dynamic-programming.aspx
dp 编程