dp 编程_使用动态编程(DP)查找丑数

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:

算法:

  1. 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。

  2. Creating a DP matrix in which ith cell represent ith ugly number.

    创建一个DP矩阵,其中第一个单元格代表第一个数字。

  3. Start filling the dp matrix from i = 2.

    i = 2开始填充dp矩阵。

  4. Ugly number(i) = minimum of (2*ugly number(f2), 3*ugly number(f3), 5*ugly number(f5)).

    数(i)=(2 *丑数(f2),3 *丑数(f3),5 *丑数(f5))的最小值

  5. 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 编程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值