General idea
1. Simplify the problem and analyze it from a relatively simple situation, such as one element, two elements, and three elements. The problem continues to increase and find the rule, this is conducive to discovering patterns, solving problems, and handling boundary conditions. For example, the beauty of programming, such as the site selection of delivery stations, and the number of problems 1 can all be solved through gradual analysis.
2. simulateProgramThe running process, such as binary search, can use four element examples to simulate the search process and summarize the judgment conditions.
3. Get a question. First, let's see if we can solve the problem directly through the Loop Based on the question. If we can solve the problem separately, the key to solving the problem is how to solve the two parts and merge them, for example, the closest point problem, the Merge Sorting Problem, array and the biggest problem (the solution on programming pearl) are similar to the recursive solution. The division and control method usually uses recursion to solve the problem. If it is a combination optimization problem, give priority to dynamic planning or greedy Solutions
4. Regarding "2", from Yin and Yang in ancient China to the front and back of dialectical materialism, "2" is of great philosophical significance. This is especially true for computers: binary in computers; divide and conquer are usually divided into two parts; there are also two half searches; many problems are either divided into two sides (Binary searches ); either divided into two sub-problems (divide and conquer), or two ends of the sub-problems, comparison Center (two pointers are used at the intersection of the linked list, the beauty of two qualified data programming methods is 2.2, and the beauty of programming is 2.21); there is also a Data Structure binary tree; when analyzing the problem, check the two ends of the problem and use two pointers may bring a lot of convenience to solve the problem. For example, you can use two stacks to implement a queue.
Dynamic Planning
1. If a problem involves an optimal solution, dynamic planning may be used to solve the problem.
2. The key to dynamic planning is to give recursive solutions to the equation, and then check whether some solutions are repeated in the recursive process. These solutions are solved from the bottom up in advance and saved, for later use.
3, if you can draw a matrix or array to save the data, the program is basically clear, because it is from the bottom up to solve the I problem, the use of the I-1 results, which is the I-1 location to access an array or Matrix
4. Dynamic Planning often design the combination explosion problem. Generally, do not consider the combination to solve the problem, because this is usually not conducive to thinking how to design the program, but we should consider how to cycle and set the Matrix to save the calculation results, through the iteration of I-1 to I to solve the problem, although the combination is a direct expression of the problem, but unfavorable and program design, because the program is finally completed through several cycles.
5. The matrix or array for storing data is usually one more row (column) than the processed data, because 0th rows (column) are the initial values and are used to calculate I = 1 (0, use I-1 data
* 6. If you use a loop to find parts and problems, the complexity is often increased by N times. In this case, you can use the result of the previous summation + the current element to reduce the number of cycles and the complexity, for example, the shipping site selection problem, the largest sub-array and the problem are very valuable.
Recursion
1. If the global or referenced variable ++ is used before recursion, if you want the variable to be restored to the previous value after recursion, then after Recursion -- the variable, such as finding the height of the tree, this policy is adopted.
2. Recursive use of the function call results. We don't have to worry about how the results are obtained. We can just use them directly, but it is not actually true, the actual calculation process is completed after recursion, and the result obtained by recursion is often advantageous to the subsequent calculation process. A typical problem is to find the distance between the last two points.
3. recursion often leads to efficiency problems. For example, the cycle speed is much higher than the recursion speed for the issue of finding the Fibonacci series, the recursive efficiency and stack size both limit the number of problems that can be solved by recursive problems.
Common sense questions
1. the position in the middle of the array is (start + end)/2, divideAlgorithmWe usually solve two problems: F (START, (start + end)/2) and F (start + end)/2 + 1, end), and half-lookup, because the middle element has been compared, so start = middle + 1; or end = middle-1, start = end to process only one element, pay special attention
2. The element elements in the array are end-start + 1
3,If a given number is required, you can obtain it by modulo 10. If you want to obtain more than 10 pieces of data, you can process it by Division of 10,
For example 123,123% 10 = 3; 123/10 = 12