[Question] P94 sword refers to offer: dynamic programming and greedy algorithm: interview question 14: cut the rope

Dynamic programming and greedy algorithm

The problems that can be solved by dynamic programming have the following three characteristics:

1. Seek the optimal solution of a problem 2. The optimal solution
of the overall problem depends on the optimal solution of each sub-problem
3. Decompose the big problem into several small problems, and there are smaller overlaps between these small problems The sub-problem
4. Analyze the problem from top to bottom. Solve the problem from the bottom up.
This is because the sub-problems reappear in the process of decomposing the big problem. In order to avoid re-solving the sub-problems, we can first calculate the optimal solution of the small problem in a bottom-up order and store it, and then obtain it based on this The optimal solution to the big problem.

Interview Question 14: Cut the rope

Topic:
Give you a rope of length n. Please cut the rope into m segments (m, n are integers, n> 1 and m> 1). The length of each segment is recorded as k[0], k[ 1], k[m], what is the maximum possible product of k[0]×k[1]×…×k[m]?
For example, when the length of the rope is 8, we cut it into lengths respectively For the three segments of 2, 3, and 3, the maximum product obtained at this time is 18.

O(n^2) time and O(n) space dynamic programming ideas

/***************动态规划算法*****************/
int MaxProductAfterCutting_Solution(int length)
{
    
    
	//定义当绳长小于2时,最大的乘积
	if (length < 2)
		return 0;
	//当绳长等于2时,最大的乘积
	if (length == 2)
		return 1;
	//当绳长等于3时,最大的乘积
	if (length == 3)
		return 2;

	//当剩下的绳长分别是0/1/2的时候,最大乘积就是本身的长度,不需要再去剪,因为一刀没有不剪的乘积大
	int* product = new int[length + 1];
	product[0] = 0;
	product[1] = 1;
	product[2] = 2;
	product[3] = 3;

	int Max_Value = 0;

	for (int i = 4; i <= length; ++i)
	{
    
    
		Max_Value = 0;

		//计算绳长为i时的最大乘积
		//因为要计算f(i)乘以f(i-j)的最大值,j超过一般是就重复
		for (int j = 1; j <= i / 2; ++j)
		{
    
    
			int products = product[j] * product[i - j];

			//如果最大乘积比当前的最大值还要大
			if (Max_Value < products)
				//将绳长为i的最大乘积记录下来
				Max_Value = products;
		}
		//更新记录表中的最大乘积
		product[i] = Max_Value;
	}

	Max_Value = product[length];
	delete[] product;
	return Max_Value;
}

int main()
{
    
    
	int length = 9;
	int result = MaxProductAfterCutting_Solution(length);
	cout << "此时得到的最大乘积是:" << result << endl;
	return 0;
}

O(1) Time and Space Greedy Algorithm Idea

/*****************贪婪算法*****************/
int MaxProductAfterCutting_Solution(int length)
{
    
    
	//当绳长不超过2时的最大乘积
	if (length < 2)
		return 0;
	//当绳长为2时的最大乘积
	if (length == 2)
		return 1;
	//当绳长为3时的最大乘积
	if (length == 3)
		return 2;

	/*********当绳长超过5时,尽可能多地把绳子剪成长度为3**********/
	//绳长为3的绳子段数
	int timesof3 = length / 3;

	//当剩下的绳长恰好为4时,则不能再剪去3,最优的解释将绳长为4剪成2*2
	//如果剩下绳长为1
	if (length - timesof3 * 3 == 1)
		timesof3--;//留下一段长度为3的绳长

	//绳长为3的绳子段数
	int timesof2 = (length - timesof3 * 3) / 2;

	//绳子的最大乘积
	return pow(3, timesof3) * pow(2, timesof2);
}

int main()
{
    
    
	int length = 9;
	int result = MaxProductAfterCutting_Solution(length);
	cout << "此时绳长的最大乘积:" << result << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114900251