Parsing algorithm commonly used - Dynamic Programming

1  Import java.util.HashMap;
 2  
3  / ** 
4  * 
 5  * Original link: https://wx.abbao.cn/a/4736-4b66e5f9ec584ee0.html 
6  * 
 7  * Take the stairs Question: Is there a height of 10 stairs stairs going up from the bottom, each stride up only level 1 or level 2 steps. Requirements for procedures to obtain a total of how many moves.
8  * 
 9  * This is an entry-level dynamic recurrent problem
 10  * 
 11  * Analysis:
 12  * 
 13  * most substructures: the last step up to the tenth floor How many ways? Two kinds: 8- "10; 9-" 10 then if there are 8 x come cases, have come to 9 y 10 can be seen in the case come y + x
 14  * 
 15  * when the state transition equation of an equation, f (10) = f (9) + f (8) both F (n-) = F (n--. 1) + F (n-2-)
 16  * 
 . 17 * Went to the second boundary layer can go one step further, you can take two steps. I.e., f (2) = 2 can only take one step a first layer F (. 1). 1 =
 18 is  * 
 . 19  * These are the problems to resolve is the modeling problem solving
 20 is   * / 
21 is  public  class WalkStair {
 22 is      public  static  void main (String [ ] args) {
 23 is          System.out.println (getWayStair1 (10 ));
 24          System.out.println (getWayStair2 (10, new new the HashMap <Integer, Integer> ()));
 25          System.out.println (getWayStair3 (10 ));
 26      }
 27  
28      / * 
29       * the first idea: a recursive manner
 30       * 
 31      * @Param NN Stair
 32       * 
 33       * It should be noted that the problem about complexity of binary length n-1, node 2 ^ n-1, n-time complexity ^ 2
 34 is       * / 
35      public  static  int getWayStair1 ( int n-) {
 36          IF (n-==. 1 ) {
 37 [              return . 1 ;
 38 is          }
 39          IF (n-== 2 ) {
 40              return 2 ;
 41 is          }
 42 is          return getWayStair1 (n--. 1) getWayStair1 + (n-- 2 );
 43 is      }
 44 is  
45     / ** 
46       * second approach: or recursion, but the repeated calculation points can not calculate, using a known algorithm memo node map that has been calculated to save
 47       * 
 48       * time complexity is O (n-)
 49       * 
 50       * @param n-
 51 is       * @param Map
 52 is       * @return 
53 is       * / 
54 is      public  static  int getWayStair2 ( int n-, the HashMap <Integer, Integer> Map) {
 55          IF (n-==. 1 ) {
 56 is              return . 1 ;
 57 is          }
 58          IF (n-2 ==) {
 59              return 2 ;
 60          }
 61 is          IF (map.containsKey (n-)) {
 62 is              return as map.get (n-);
 63 is          } the else {
 64              int value = getWayStair1 (n--. 1) getWayStair1 + (n-- 2 );
 65              map.put (n-, value);
 66              return value;
 67          }
 68      }
 69  
70      / ** 
71 is       * a third path: dynamic programming, only the results of each obtained two times and the results of time complexity o ( n) space complexity O (. 1)
 72       * 
 73 is       * @param n-
74      * @return
75      */
76     public static int getWayStair3(int n) {
77         if (n == 0) {
78             return 0;
79         }
80         if (n == 1) {
81             return 1;
82         }
83         if (n == 2) {
84             return 2;
85         }
86 
87         int a = 1;
88         int b = 2;
89         int temp = 0;
90         for (int i = 3; i <= n; i++) {
91             temp = a + b;
92             a = b;
93             b = temp;
94         }
95         return temp;
96     }
97 }

 

1  / * 
2  * Problem Description: digging five ten gold, gold seeking the most number dug
 3  * 
 4  * ideas: optimal substructure: '1: Block Five gold digging' 2: The gold does not dig five boundary: '1: number of gold enough to dig a' 2: digging a sufficient number of state transition equation gold
 . 5  *
 . 6   * / 
. 7  public  class Goldminer {
 . 8      / ** 
. 9       * 
 10       * @param n- n ore front seat
 . 11       * @param number w worker
 12 is       * @param number of workers required for mine p each block
 13 is       * @param number of each block of gold ore G
 14       * @return 
15       * / 
16      public static  int getMaxGold ( int n-, int W, int P [], int G []) {
 . 17  
18 is          // 'is a boundary value if the first three 
. 19          if (n-<. 1 ) {
 20 is              return 0 ;
 21 is          }
 22 is          if ( W ==. 1 && n-<P [0 ]) {
 23 is              return 0 ;
 24          }
 25          IF (n-&&. 1 == W> = P [0 ]) {
 26 is              return G [0 ];
 27          }
 28  
29          //'This is the result of the initialization 
30          int [] preResult = new new  int [W +. 1 ];
 31 is          int [] Result = new new  int [W +. 1 ];
 32          for ( int I = 0; I <W; I ++ ) {
 33 is              IF (W> = P [0 ]) {
 34 is                  preResult [I] = G [0 ];
 35              } the else {
 36                  preResult [I] = 0 ;
 37 [              }
 38 is          }
 39  
40          / ** 
41 is          * 'Of the outer loop to dig several gold, the inner layer of a first control loop may be dug optimal results, the inner layer of the second refresh cycle may achieve optimal results, and returns the current number of people could dig optimal results
 42 is           * / 
43 is          for ( int I = 0; I <n-; I ++ ) {
 44 is              for ( int J = 0; J <= W; J ++ ) {
 45                  IF (J < P [I]) {
 46 is                      result [J ] = preResult [J];
 47                  } the else {
 48                      Result [J] = Math.max (preResult [J], preResult [J - P [I]] + G [I]);
 49                  }
 50              }
 51 is              for ( int j = 0; j <= w; j++) {
52                 preResult[j] = result[j];
53             }
54         }
55         return result[w];
56     }
57 
58     public static void main(String[] args) {
59         int[] g = { 400, 500, 200, 300, 350 };
60         int[] p = { 5, 5, 3, 4, 3 };
61         System.out.println(getMaxGold(5, 10, p, g));
62     }
63 }

 

. 1  Import of java.util.ArrayList;
 2  Import java.util.List;
 . 3  
. 4  / ** 
. 5  * problem:
 . 6  * the Given String A non-empty S and A Dictionary of List A containing wordDict
 . 7  * non-empty words, the Determine S segmented iNTO CAN BE IF a Space-Separated
 . 8  * Sequence of One or More Dictionary words.
 . 9  *
 10   * / 
. 11  public  class Lc139 {
 12 is      / ** 
13 is       * idea: dp on a string to the current position of the position taken string It happens to be a string dictionary
 14       * @param S
 15       * @param wordDict
16      * @return
17      */
18     public static boolean wordBreak(String s, List<String> wordDict) {
19         boolean[] dp = new boolean[s.length() + 1];
20         dp[0] = true;
21 
22         for (int i = 1; i < dp.length; i++) {
23             for (int j = 0; j < i; j++) {
24                 if (true == dp[j] && wordDict.contains(s.substring(j, i))) {
25                     dp[i] = true;
26                 }
27             }
28         }
29         return dp[s.length()];
30     }
31 
32     public static void main(String[] args) {
33         String s = "leetcode";
34         String s1 = "leet";
35         String s2 = "code";
36         List<String> wordDict = new ArrayList<String>();
37         wordDict.add(s1);
38         wordDict.add(s2);
39         System.out.println(wordBreak(s, wordDict));
40     }
41 }

 

Guess you like

Origin www.cnblogs.com/xiaoshahai/p/11800080.html