Today's topic:
- Fibonacci sequence
- Frog jumping Step problem (and its variants: abnormal jumping steps)
- Rectangle Overlay
- Minimum number of rotated array
- Paths in The Matrix
- Range of motion of the robot
Careful classmates will find that the first question is actually corresponding to the "sword refers to" the book on the same topic, that is, the 10th Fibonacci series, this kind of problem belongs to the recursive problem, although the idea is relatively simple, but it is the kind of not to see the answer to think about it, see the answer to the question, So in peacetime students and bloggers should practice this type of topic more, training this recursive thinking. Interestingly, bloggers found that the three topics can be solved with dynamic planning, and often the time spent on dynamic planning is lower than recursion's. This is illustrated in the following example with a rectangle overlay.
5th, 62 is the problem of backtracking, there are many of this type of problem in Leetcode, as long as more practice, this type of topic is relatively easy to get started. This article only describes question 6th.
3. Moment line Coverage
Title Description:
We can use the small rectangle of 2*1 to cover the larger rectangle horizontally or vertically. What is the total number of ways to cover a large rectangle of 2*n with n 2*1 small rectangles without overlapping?
Ideas:
Taking N=8 as an example, we first put the overlay method of the 2*8 rectangle as f (8). There are two options when using a 2*1 small rectangle to cover the leftmost side of a large rectangle: vertical or horizontal. If you choose to put it vertically, then the 2*7 area is left on the right, which is F (7), and if it is placed horizontally in the upper left corner, a rectangle must also be placed in the lower left corner, then the 2*6 area is left on the right, i.e. F (6). So we have F (8) =f (7) +f (6). From this side we can see that this is actually a Fibonacci sequence.
After clearing the train of thought, we use recursive and dynamic programming methods to achieve the following.
The recursive code is as follows:
1 Public classSolution {2 Public intRectcover (inttarget) {3 if(target = = 0)return0;4 if(target = = 1)return1;5 if(target = = 2)return2;6 returnRectcover (target-1) +rectcover (target-2);7 }8}
The code is simple and clear, but the efficiency is very low, his time complexity is not difficult to roll out to be exponential. The operating time on the Ox-Guest network is 447ms.
Let's take a look at how dynamic planning is resolved with the following code:
1 Public classSolution {2 Public intRectcover (inttarget) {3 if(target = = 0)return0;4 int[] DP =New int[Target+1];5Dp[0] = 1;6DP[1] = 1;7 for(inti = 2; I <= target; i++){8Dp[i] = dp[i-1] + dp[i-2];9 }Ten returnDp[target]; One } A}
Obviously, the time complexity of this code is only O (n), and of course the spatial complexity is O (n), so you can do the same:
1 Public classSolution {2 Public intRectcover (inttarget) {3 if(target = = 0)return0;4 intNUM1 = 1;5 intnum2 = 1;6 intres = 1;7 for(inti = 2; I <= target; i++){8res = NUM1 +num2;9NUM1 =num2;Tennum2 =Res; One } A returnRes; - } -}
So the complexity of space is only constant. Dynamic planning runs at only 12ms in the same conditions.
4. Rotate the minimum number of the array
Title Description: Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. Note: All elements given are greater than 0, and if the array size is 0, return 0.
This question in the leetcode above is the original question, when the blogger does not have too many ideas, directly from the back and forward the array, if a number is smaller than the number in front of it, then this number is the smallest, the time complexity of O (n). The code is as follows:
1 Public classSolution {2 Public intMinnumberinrotatearray (int[] Array) {3 if(Array.Length ==0)return0;4 for(inti = array.length-1; I >0; i--){5 if(Array[i-1] >Array[i])6 returnArray[i];7 }8 returnArray[0];9 }Ten}
But in the "sword", the question has a time complexity for O (logn) of the idea: the use of binary search ideas to find the smallest number, the specific process is too complex to elaborate, interested students can read the book on the 83rd page, this side only posted code:
1 Public classSolution {2 Public intMinnumberinrotatearray (int[] Array) {3 if(Array.Length ==0)return0;4 ints = 0;5 intE = array.length-1;6 if(Array[s] <Array[e]//= rotated 0 digits to the back of the array7 returnArray[s];8 while(s! =e) {9 if(E-s = = 1)Ten returnArray[e]; One intMid = (s+e)/2; A if(Array[mid] = = Array[s] && Array[mid] = =array[e])//Handling Special cases - returnOrderfind (array,s,e); - if(Array[mid] >=Array[s]) thes =mid; - Else if(Array[mid] <=Array[e]) -E =mid; - } + returnArray[s]; - } + Public intOrderfind (int[] Array,intStartintend) { A intres =Array[start]; at for(intN:array) { -res = (n < res)?N:res; - } - returnRes; - } -}
6. Motion range of the robot
Title Description: There is a M-row and N-column squares on the ground. A robot moves from the grid of coordinates 0,0, each time only to the left, right, upper and lower four directions, but cannot enter the coordinates of the row and column coordinates of the sum of the squares greater than K. For example, when K is 18 o'clock, the robot can enter the square (35,37) because 3+5+3+7 = 18. However, it cannot enter the grid (35,38) because 3+5+3+8 = 19. How many grids can the robot reach?
Ideas:
This is a more straightforward problem of backtracking, first using an array to mark the robot has traversed the song, to prevent repetition, and then use backtracking to let the robot movement, each movement to a square to determine whether it is legal, if legal, the counter and then go down; otherwise returns 0;
The code is as follows:
1 Public classSolution {2 Public intMovingcount (intThresholdintRowsintcols)3 {4 Boolean[] visited =New Boolean[Rows][cols];5 returnMovingcount (threshold,visited,rows,cols,0,0);6 }7 8 Public intMovingcount (intKBoolean[] visited,intRowsintcols,9 intRowintCol)Ten { One if(Row < 0 | | row >= rows| | ACol < 0 | | Col >= cols | |Visited[row][col]) - return0; -Visited[row][col] =true; the intsum = 0; - intRow_tmp =Row; - intCol_tmp =Col; - Do{ +Sum + = (row%10); -Row/= 10; +} while(Row > 0); A at Do{ -Sum + = (col%10); -Col/= 10; -} while(Col > 0); -row =row_tmp; -Col =col_tmp; in if(Sum >k) - return0; to Else{ + returnMovingcount (K,visited,rows,cols,row+1,col) + -Movingcount (K,visited,rows,cols,row-1,col) + theMovingcount (k,visited,rows,cols,row,col+1) + *Movingcount (K,VISITED,ROWS,COLS,ROW,COL-1) +1; $ }Panax Notoginseng } -}
The third day of the arithmetic problem of "Jian Point offer"