"The Beauty of Programming" Reading Notes II

PB15151793 陈灿


Today, I took the time to read the second chapter "The Charm of Numbers" of "The Beauty of Programming", and finished the third chapter "The Method of Structure". Why in such a hurry? Because it feels that the interview may come at any time, it is better to prepare early.

 

divergent thinking

I feel that the book "The Beauty of Programming" actually cares about a kind of divergent thinking , that is, if we have solved problem A now, can we change some conditions to generate a new problem B, and then try to solve problem B. Generally speaking, the conditions to be changed are some of the constraints we added to the question before, and we can generalize the problem by removing these constraints. Of course it will be more difficult to solve. I think this is a very important habit of thinking, which can help us become familiar with a class of topics in a short period of time and truly achieve mastery.
Let's take an example:

 求N个元素的一维数组中子数组和的最大值。

Here I will not go around the corners and give the O(N) solution directly. This problem, if you think about it from the front to the back, involves the update of the array is very troublesome. But if you think from the back to the front, it will give people a feeling of darkness. Note that the maximum sum array ending at subscript i is S[i], then when we want to get S[i+1], we only need to see if S[i] is greater than 0, and if it is greater than 0, add A[i+ 1], as S[i+1], otherwise only A[i+1] is considered as S[i+1]. The focus is actually on the back, the key is to observe how the author does this extension and how to deal with the conditions of change. The author made an extension to the title, that is, the head and tail of this array can be connected, and it is still the maximum value of the sum of the sub-arrays. Here we actually only need to increase the analysis of one situation, that is, A[0] and A[N-1] are connected, that is, to find the largest subarray starting at A[0] and ending at A[N -1] for the largest subarray. The latter is easy to obtain. For the former, we actually only need to compare each subarray starting with A[0]. Just pay attention to some details when merging. What impresses me most is that the author has extended the original problem here, namely

求NxM个元素的二维数组中子矩阵和的最大值。

For this question, how should we analyze Ni? In fact, in general, the solution of this kind of expansion problem depends on the solution of the original problem. Then, the most important thing is to grasp the connection and difference between the original problem and the extension. The connection is to find the submatrix and the maximum value (one dimension of the original problem submatrix is ​​1). The difference is that the dimensions of the two problems are different, so what operation can we use to reduce the dimension of the expansion problem? The author does this here, first determine 2 rows, assuming that the upper and lower boundaries of the sub-matrix are these two rows, and then regard the sum of each column as the corresponding value in the one-dimensional array, so that the operation of the problem can be used. The complexity here is O(N^2*M), so let's think about the nature of the dimensionality reduction that can be reduced to sub-problems? Why can we convert the column sum to a one-dimensional problem here? My answer is, because this is a two-dimensional matrix, an element in a column is determined, and of course this column must be selected. Some people may think that my question is mentally handicapped, but I think the most simple reason is that such a mentally handicapped, only when the depth of thinking about the problem reaches the level of "mentally handicapped", can we truly understand this problem.
After that, the author made a lot of extensions. For example, two-dimensional matrices can be connected left and right, even up and down, and even extended to three-dimensional matrices. But I think that all changes are inseparable, as long as the essence of the problem is grasped, such problems can be solved.

 

hardest simple question

I feel that MS actually attaches great importance to the ability of the interviewee to solve some simple problems with actual coding during the interview. Because it feels that a very common situation is that when we usually experiment on the computer, many people will look at the routines of the textbook to write programs, which is very bad. Because some code looks simple, but in fact you write a lot of pitfalls. If this kind of code, you just look at the textbook, you will feel very simple at the time, because from the ideological point of view, it is really simple. But once you leave the textbook, nothing is left. For example, in a binary search problem, to find the subscript of the first value greater than val in an ordered array, there are really a lot of pits in it. This is a question I encountered when I interviewed for MSRA. Not to mention, it was full of tears. In addition, there are also very basic textbook topics that will also be asked, such as writing a program to determine the construction of a binary tree from a preorder sequence and an inorder sequence. Although this question is a textbook problem, the company will really Ask, don't think this kind of question is old-fashioned. Questions that reflect basic skills are generally old-fashioned questions. For this kind of problem, the place where it is easy to make mistakes is often the boundary point, which I do not support at all: write an outline first, and then adjust the boundary problem. Because people who hold this kind of thinking must not understand the meaning of each variable. The process of coding reflects a person's process of thinking about problems. If the interviewer knows the meaning of each variable (specifically, the boundary value here), then the program he writes must be rigorous in dealing with boundary problems! Such mathematical and logical thinking is worth affirming!

 

Linked list catch-up, ring judgment problem

I feel that this buddy wrote very well, so I will not write it. .
Summary of linked list problems

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324977107&siteId=291194637