Summary of Ideas for Answering Algorithmic Questions in Interview

In interviews, the purely algorithmic questions are generally hated by many programmer friends. Here are some ideas and techniques for solving algorithmic questions.

Generally, articles about algorithms start from classic algorithms. One kind of algorithm introduction. If you see more algorithms, you will naturally gain insights. However, the time and energy spent on such learning is too great, and it is not suitable for blogs. Communicate inside. This article is dedicated to quick ideas. When faced with algorithmic problems, many people are almost completely blank in their minds. This article is about starting from the problem and opening up a gap in the problem with no ideas. Kind of common skills.

(1) From simple to complex

In fact, many problems are really difficult to get the right idea in the first time. At this time, you can try a simple to complex idea. First, reduce the size of the question to the point where it is very easy to answer.

[Question] There are enough coins for 2 cents, 5 cents, and 1 cent. How many ways are there to collect 1 yuan?

At first glance, this question seems to be completely impossible to start, but according to the idea from simple to complex, we can first consider the extremely simple situation. If the size of the question is reduced to: there are enough 1 cent coins, please collect 1 How many ways are there to divide money? There is no doubt that the answer is 1.

After getting this answer, we can slightly expand the scale of the question: There are enough 1 cent coins, how many ways are there to collect 2 cents? How many ways are there to collect n cents? The answer is still 1

Next, we can expand the problem from another angle. With enough 1 cent coins and 2 cent coins, how many ways are there to collect n cents? At this time, we already have a sufficient amount of 1 cent coins in our hands. There is only one way to collect any amount of money. Then only one cent can be used to collect n-2 cents. There is one way, only 1 cent. There is 1 way to collect n-4 cents with money, and 1 way to collect n-6 cents with only 1 cent...

Collecting these n-2, n-4, and n-6 ​​sums of money and making up 2 cents each will produce a new method of collecting n cents. The total number of these methods is +1, which means 1 Div coins and 2 cent coins, and counted the ways to collect n cents.

In the interview, it is a very useful attempt to adopt this kind of thinking immediately. Solving small-scale problems can make you more familiar with the problem and slowly discover the characteristics of the problem. The most important thing is to give your interviewer a positive signal—— Analyzing the problem immediately looks much better than frowning.

For this problem, we can quickly find that the scale of the problem has two dimensions: using a1-ak coins and collecting n cents, so we can record it as P(k,n). When we find the recursive formula P(k,n) = P(k-1,n-ak) + P(k-1,n-2 ak) + P(k-1,n-3 ak)…… This problem has been solved

Usually simple to complex ideas are very effective for solving dynamic programming problems. When a certain amount of solutions to simple problems are accumulated, the answers to higher-level problems are often already in front of them.

(2) One is divided into two

Another way of thinking is to cut the problem with one knife, divide the problem into two halves, and turn it into two problems with the same structure as the original problem. If you can divide the problem into two, you can divide it into four, and then you can again. One is divided into 8 until it is divided into problems that we can easily solve. When trying this kind of thinking, in fact, only two questions need to be considered: 1. After one is divided into two, has the problem been simplified? 2. According to the solution of the two problems divided into two, can the solution of the whole problem be easily obtained?

[Title] Sort an array.

This classic algorithm is definitely familiar to everyone and can no longer be familiar, but if you think about this problem from the beginning, not everyone can come up with one of several classic sorting algorithms. This is just an example to illustrate The application of the idea divided into two.

The simplest way to divide into two is to divide the array in half and sort them separately. For two ordered arrays, we have a way to merge them into an ordered array, so the idea of ​​dividing into two is feasible, and for an array that has been divided into two halves, we can also divide the number into two halves , Until we divide the array with only one element, the one-element array is naturally ordered. It is not difficult to see that according to this kind of thinking, what we get is the "merge sort" in the classic array sorting algorithm.

There is another way of dividing into two. Considering that it is more complicated to divide the array into two halves and merge it together, we can consider dividing the array into two halves according to a certain element greater than and less than, so that as long as they are resolved separately, they can be directly connected into An ordered array is now, and the same problem can be divided into two again. According to this idea, the "quick sort" in the classic array sorting algorithm can be derived.

(3) Turning the virtual into reality

This kind of thinking is aimed at special problems related to floating-point numbers, because no matter it is exhaustive or dichotomy, it is difficult for calculation problems related to floating-point numbers (especially computational geometry) to be effective, so turning virtual into real refers to "Virtual" floating point numbers are replaced by integers. The specific method is to sort some of the floating-point numbers given in the title (not limited to floating-point numbers, we do not care about their specific size integers), and then use the serial number of the floating-point number instead of itself to think about the problem, and then replace it when the specific calculation come back.

[Title] Given n rectangles with horizontal and vertical sides (represented by a quadruple [x1,y1,x2,y2]), find their total coverage area.

Because the coordinates may appear as floating-point numbers, this question looks very complicated (you can practice the above ideas from simple to complex and dividing into two are basically invalid), think a little, the coverage relationship of the rectangle is actually only related to the size of the rectangle coordinates , So we try to think about sorting all the x values ​​of the rectangles, and then use the serial number to replace the specific vertical, and the y value is also the same, so we get that all the rectangles are actually in a 2nx2n block, so we use the simplest method of poor organization, It can be calculated whether each 1x1 grid is covered. So far, as long as we calculate the area and convert the real length and width of the grid back, we have already got the answer to the question.

Guess you like

Origin blog.csdn.net/S11035762/article/details/109310476
Recommended