dp entry example (1)

Masseur problem

https://leetcode-cn.com/problems/the-masseuse-lcci/

(Find a good state transition equation) 

Last state and only relevant today, is still the discussion classification:

    not accept a reservation today: yesterday or not to accept the reservation, or the last accepted reservation, whichever is the maximum value, namely: dp [i] [0] = max ( dp [i - 1] [0 ], dp [i - 1] [1]);
    by appointment today: just do not accept an appointment from yesterday transferred from, coupled with today's often, namely: dp [i] [? 1] = dp [i - 1 ] [0] + nums [i]

. 1  public  class Solution {
 2  
. 3      public  int Massage ( int [] the nums) {
 . 4          int len = nums.length;
 . 5          IF (len == 0 ) {
 . 6              return  0 ;
 . 7          }
 . 8          IF (len == . 1 ) {
 . 9              return the nums [ 0 ];
 10          }
 . 11  
12 is          // DP [i] [0]: the interval [0, i] in the reservation request accepted, and the subscript i to this day not accept reservation maximum duration
 13          //dp [i] [1]: the maximum time interval [0, i] in receiving the reservation request, and the subscript for that day accepted i reservation of 
14          int [] [] DP = new new  int [len] [ 2 ];
 15          DP [ 0 ] [ 0 ] = 0 ;
 16          DP [ 0 ] [ . 1 ] = the nums [ 0 ];
 . 17  
18 is          for ( int I = . 1 ; I <len; I ++ ) {
 . 19              DP [I] [ 0 ] = the Math .max (DP [I - . 1 ] [ 0 ], DP [I - . 1 ] [ . 1 ]);
 20 is             dp[i][1] = dp[i - 1][0] + nums[i];
21         }
22         return Math.max(dp[len - 1][0], dp[len - 1][1]);
23     }
24 
25     public static void main(String[] args) {
26         Solution solution = new Solution();
27         // int[] nums = {1, 2, 3, 1};
28         // int[] nums = {2, 7, 9, 3, 1};
29         int[] nums = {2, 1, 4, 5, 3, 1, 1, 3};
30         int res = solution.massage(nums);
31         System.out.println(res);
32     }

 

 

 

 

 

 

 

 


Author: liweiwei1419
link: https: //leetcode-cn.com/problems/the-masseuse-lcci/solution/dong-tai-gui-hua-by-liweiwei1419-8/
Source: stay button (LeetCode)
copyright reserved by the authors . Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/zhmlzhml/p/12639643.html