Dynamic programming to solve the maximum product series problem (rolling violent enumeration) java

content

1. Examples

Topic description

2. Thought analysis

basic idea

Specific steps

3. Code implementation

4. DP summary

Notice


1. Examples

Topic description

Given a substring S of n elements, you need to find a consecutive substring with the largest product.

If this largest product is not positive, it should output 0 (indicating no solution).

enter

The first line is an integer n (1<=n<=18).

The second line is n integers. where each integer is in the range [-10,10].

output 

Output a line containing an integer, output the largest product.

Test Data 

See code.

2. Thought analysis

basic idea

The topic is similar to the knapsack problem, and its basic idea can be used dynamic programming: remember the solutions to the sub-problems that have been solved. Solving, that is, when traversing the data, the maximum product obtained during the traversal process is recorded, and then the traversal is continued. Through each traversal, the maximum product is dynamically updated , and the final solution is finally obtained.

For such questions, two variables, max and min, can be defined to record the maximum and minimum values ​​in the process. cur represents the current data to be calculated in the calculation process. In each calculation process, max takes the maximum value of Max.( cur, cur*max, cur*min ), and min takes the minimum value of Min.( cur, cur*max, cur*min ).

Specific steps

1) Initialize cur, max, min data, the default is the first number.

2) Loop through the array elements.

3) Each time the loop max, min are multiplied by the current element cur. and save it with a temporary variable.

4) Calculate the new max and min and reassign them.

5) The new maximum value ans=Max(max, ans) in the calculation process.

6) After traversing, print ans.

3. Code implementation


import java.util.Scanner;

public class Main {
	static long[] nums;// 用来存放数据

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		/**
		 * long cur 当前数据 long max 最大数 long min 最小数 long ans 最终结果
		 */
		long cur, max, min, ans = 0;
		// 录入数据
		for (int i = 0; i < m; i++)
			nums[i] = scanner.nextLong();
		// 初始化数据,默认都为第一个数
		cur = nums[0];
		max = nums[0];
		min = nums[0];
		for (int i = 1; i < m; i++) {
			cur = nums[i];// 当前处理的数
			long tempmax = max * cur;// 当前数cur乘以最大数max,定义一个临时变量tempmax储存
			long tempmin = min * cur;// 当前数cur乘以最小数min,定义一个临时变量tempmin储存
			// 新的max在上面三个数中产生
			/* 因为max在下一步求min时还要用到,因此这里定义一个临时变量储存newmax */
			long newmax = Math.max(cur, Math.max(tempmax, tempmin));
			min = Math.min(cur, Math.min(tempmax, tempmin));
			max = newmax;// max使用完后,将新的newmax赋值给max
			ans = Math.max(ans, max);// 数据处理过程中的答案在ans和max中产生
		}
		System.out.println(ans);
	}

}

4. DP summary

Notice

The core idea of ​​the dynamic programming algorithm is to divide the big problem into small problems to solve, so as to obtain the optimal solution step by step. The
dynamic programming algorithm is similar to the divide and conquer algorithm, and its basic idea is to decompose the problem to be solved. into several sub-problems, first solve the sub-problems, and then obtain the solution of the original problem from the solutions of these sub-problems.
Different from the divide-and-conquer method, the sub-problems obtained by decomposing are often not independent of each other for problems that are suitable for solving by dynamic programming. (That is, the solution of the next sub-stage is based on the solution of the previous sub-stage, and further solutions are carried out.)
Dynamic programming can be gradually advanced by filling in the form to obtain the optimal solution.

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123533029