A famous masseuse receives a steady stream of requests for appointments, each one optional or not. There is a break between each appointment, so she cannot accept adjacent appointments. Given a sequence of appointment requests, find the optimal set of appointments for the massage therapist (with the longest total appointment time) and return the total number of minutes. Example 1: Input: [1,2,3,1] Output: 4 Description: Select the reservation number 1 and 3. The total duration = 1 + 3 = 4. Example 2: Input: [2,7,9,3,1] Output: 12 Description: Select reservation 1,3, and 5. The total duration = 2 + 9 + 1 = 12. Example 3: Input: [2,1,4,5,3,1,1,3] Output: 12 Description: Select 1,3,5, and 8. The total duration = 2 + 4 + 3 + 3 = 12. Dynamic planning: Do a good job in business, work the longest time, but to combine work and rest, can not be tired of their own.Copy the code

1. The code is as follows: Massage. Java:

package com.yuhl.right.leetcode;

/ * * *@author yuhl
 * @Date2020/10/25 8:05-8:05-7:45-7:45-7:25-7:25-7:05-7:05-6:45-6:45 *@Classname Massage
 * @Description* A reputable masseur receives a steady stream of requests for appointments, each with the option to take or not take. There is a break between each appointment, so she cannot accept adjacent appointments. Given a sequence of appointment requests, find the optimal set of appointments for the massage therapist (with the longest total appointment time) and return the total number of minutes. * Example 1: * Input: [1,2,3,1] * Output: 4 * Description: Select appointment 1 and appointment 3. The total duration = 1 + 3 = 4. * * Example 2: * Input: [2,7,9,3,1] * Output: 12 * Description: Select reservation 1, reservation 3, and reservation 5. The total duration = 2 + 9 + 1 = 12. * * Example 3: * Input: [2,1,4,5,3,1,1,3] * Output: 12 * Description: Select Reservation 1,3,5, and 8. The total duration = 2 + 4 + 3 + 3 = 12. * /
public class Massage {
    public static void main(String[] args) {
        int[] nums = {2.1.4.5.3.1.1.3};
        int res = massage(nums);
        System.out.println(res);
    }
    public static int massage(int[] nums) {
        // Do a good job in business and work for the longest time, but you should combine work with rest and not strain yourself.
        Max (dp[i-1],dp[i-2]+nums[I]);
        int dpi_1 = 0;
        int dpi_2 = 0;

        for(int i=0; i<nums.length; i++){
            int dpi = Math.max(dpi_1,dpi_2+nums[i]);
            dpi_2 = dpi_1;// This line and the next line are the result of the hand
            dpi_1 = dpi;// Return dpi. In the dpi_1
        }
        returndpi_1; }}Copy the code

2. Execution Results:

"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java exe" 
12
Copy the code