Likou-analysis of the thinking of the masseur

Title description:

A well-known masseur will receive a steady stream of appointment requests, and each appointment can choose to pick up or not. There must be a break between each appointment service, so she cannot accept adjacent appointments. Given an appointment request sequence, find the optimal set of appointments for the masseur (the longest total appointment time), and return the total number of minutes.

Note: This question is slightly modified from the original question

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Select No. 1 appointment and No. 3 appointment, total duration = 1 + 3 = 4.
Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Select No. 1, No. 3 and No. 5 appointments, total duration = 2 + 9 + 1 = 12.
Example 3:

Input: [2,1,4,5,3,1,1,3]
Output: 12
Explanation: Select No. 1, No. 3, No. 5 and No. 8 appointments, total duration = 2 + 4 + 3 + 3 = 12.

Problem-solving idea 1 : I first considered using greedy algorithm or dynamic programming for this problem, and then every time I choose the longest time, I should get the overall optimal solution, but in the end I found that I want to solve this kind of problem the most The easy-to-understand idea is of course a brute force enumeration algorithm, which lists every possible result, and then compares them one by one to find the maximum value.
Encountered problem 1 : This idea is good, but the detection is overtime, and the huge amount of data cannot withstand my list and comparison.

    public int massage(int[] nums) {
    
    
        int res = findMax(0,nums);
        return res;
    }
    //递归枚举
    public int findMax(int i,int[] arr){
    
    
        int res = 0;
        //当选择超出预约总数时返回0
        if (i >= arr.length){
    
    
            return res;
        }
        //否则一一比较
        for (int j = i; j < i+3; j++) {
    
    
            if (j == arr.length){
    
    
                break;
            }
            int t = j+2;
            int temp = arr[j]+findMax(j+2,arr);
            //System.out.print(temp+"-");
            if(temp > res){
    
    
                res = temp;
            }
        }
        //System.out.println("->"+res);
        return res;
    }

The premise of this recursion is that the massage therapist considers appointments outside of the rest, at most, it is impossible to push down three times in a row. This should be understood well, this is the core of the algorithmic thinking.

Problem-solving idea 2 : My overall idea is okay. My next improvement should definitely be to improve the brute force algorithm, do less wasteful work, and record the optimal path that has been sought, so that it is unnecessary Iteratively solve a path many times. So I created a record array brr[ ], first set all the arrays to -1, because the appointment time for massage definitely cannot be a negative number, so I only need to judge later whether the value at brr[i] is -1. It can be known whether the path has been optimally solved.
Insert picture description here

    int brr[] = new int[200];
    public int massage(int[] nums) {
    
    
        for (int i = 0; i < brr.length; i++)
            brr[i] = -1;
        int res = findMax(0,nums);
        return res;
    }
    public int findMax(int i,int[] arr){
    
    
        int res = 0;
        if (i >= arr.length){
    
    
            return res;
        }
        for (int j = i; j < i+3; j++) {
    
    
            if (j == arr.length){
    
    
                break;
            }
            if (brr[j] == -1){
    
    
                brr[j] = arr[j]+findMax(j+2,arr);
            }
            System.out.print(brr[j]+"-");
            if(brr[j] > res){
    
    
                res = brr[j];
            }
        }
        System.out.println("->"+res);
        return res;
    }

Originally, I didn’t set all the record arrays to -1, because I thought it was impossible for someone to make an appointment for a 0-minute massage. I didn’t expect that the test case of the subject was really maddening. Not only did there be an appointment for 0 minutes, but also all users who made an appointment for 0 minutes. Yes, if this is the case, my record array is basically abolished, and I am back to the first unoptimized algorithm. Each path must be repeatedly found many times. That's why I set all the record arrays to negative numbers before calling the algorithm.

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/107485773