Dynamic programming is a kind of programming idea which is put forward to solve the problem that recursive calls occupy more memory and time complexity. Among them, the more classical is to solve the problem of low computational efficiency of Fibonacci sequence. If recursive call is used to find the nth digit of Fibonacci sequence, then according to this method, the digit will be divided into other two digits according to the formula. The two digits split will continue to be split into smaller two digits until the Fibonacci number of the first two known digits of the sequence is split. However, the problem is that if we keep splitting like this, the number that was originally calculated under another branch of the tree has not been stored, so that we do the inefficient work of repeated calculation, which greatly delays the execution time of the program. So far, we have come up with a solution to the problem - to store the calculated numbers in an array, which is called memo method.
public class DP { /** * Fibonacci series */ // /** // * Note: top down (recursive) // * @param n // * @return // */ // public static int f(int n) { // if (n == 0) { // return 1; // } // if (n == 1) { // return 1; // } // return f(n - 1) + f(n - 2); // } // The n th power of O is 2 // /** // * Note: dynamic planning (memorandum method) // * That is, the original calculated value is saved // * That is, send an array in // * @param n // * @return // */ // public static int f(int n,int[] bak) { // //int[] bak defined here is memo // // if (n == 0) { // return 1; // } // if (n == 1) { // return 1; // } // // if(bak[n] !=-1) { // //If n is 0, we initialize the memo to - 1; // //If the nth element is not - 1, then we have saved - 1. // // return bak[n]; // //If there is the nth value (that is, the nth value has been calculated), // //Let's not calculate any more, but return this value directly. // } // // // //If the nth value is not found, then // int result=f(n - 1,bak) + f(n - 2,bak); // //Don't forget to pass in the memo // // bak[n]=result; // //Once calculated values are saved // // return result; // } /** * Note: bottom up * @param n * @return */ public static int f(int n) { //int[] bak defined here is memo if (n == 0) { return 1; } if (n == 1) { return 1; } // if(bak[n] !=-1) { // //If n is 0, we initialize the memo to - 1; // //If the nth element is not - 1, then we have saved - 1. // // return bak[n]; // //If there is the nth value (that is, the nth value has been calculated), // //Let's not calculate any more, but return this value directly. // } // // // //If the nth value is not found, then // int result=f(n - 1,bak) + f(n - 2,bak); // //Don't forget to pass in the memo // // bak[n]=result; // //Once calculated values are saved // return result; // } // //Although the complexity is reduced, recursion is still used int result=0; int r1 =1; int r2 =1; for(int i=2;i<=n;i++) { result=r1+r2; r1=r2; r2=result; } return result; } public static void main(String[] args) { int n = 5; // int[] bak=new int[n+1]; / / pass the memo in // //Don't forget to initialize the array in the memo to - 1 (the previous default is 0) // for(int i=0;i<=n;i++) { // bak[i]=-1; // } // System.out.println(f(n,bak)); System.out.println(f(n)); } }
As we can see here, the point of this method is to pass the memo in and initialize the array in the memo to - 1. Here, one is to store the calculated value in the memo during the calculation of the sequence, and the other is to judge whether the required number is in the sequence. At the same time, after judging that there are bottom n values in the return value, there is no need to calculate any more.