Meeting room use longest time - Glory written test

A meeting room can be reserved within the time period of 8:00 am and 23:00. This time period can receive n appointments. The appointment format is start_i, end_i. As a secretary, you must ensure that the meeting room can be used for the longest time

Input: The first line is T, indicating that there are T groups of test cases, the second line indicates that there are n appointments, and the rest indicate the appointment start and end
1
6
15 17
8 11 10
16 11
12 13
15
9 12

Output: 8

My AC solution:

import java.util.*;

public class Main {
    
    

    static class Meeting implements Comparable{
    
    
        public int start;
        public int end;

        public Meeting(int start, int end) {
    
    
            this.start = start;
            this.end = end;
        }

        @Override
        public int compareTo(Object o) {
    
    
            Meeting o1 = (Meeting) o;
            return start-o1.start;
        }
    }

    public static void findMaxDuration(List<Meeting> meetingList){
    
    		
    	// 按开始时间排序
        Collections.sort(meetingList);
        // 动态规划
        int[] dp = new int[meetingList.size()];
        dp[0] = meetingList.get(0).end-meetingList.get(0).start;
        
        for (int i = 1; i < dp.length; i++) {
    
    
            int curDuration = meetingList.get(i).end-meetingList.get(i).start;
			// 和在前面所有不冲突的会议比较,找出最长时间(相当于找一个最长时间的路径)
            for (int j = i-1; j >= 0;j--) {
    
    
                if (doAdd(meetingList.get(j),meetingList.get(i))){
    
    
                    dp[i] = Math.max(dp[i],dp[j]+curDuration);
                }
            }
            // 存在一种情况:如果前面的都冲突,那么只能加上自己的duration
            dp[i] = Math.max(curDuration,dp[i]);
        }
        
        int max = 0;
        for (int i = 0; i < dp.length; i++) {
    
    
            max = Math.max(max,dp[i]);
        }
        System.out.println(max);

    }
    // 判断会议是否冲突
    public static boolean doAdd(Meeting lastMeeting,Meeting curMeeting){
    
    
		// 注意大于等于
        if(curMeeting.start>=lastMeeting.end){
    
    
            return true;
        }
        return false;
    }



    public static void main(String[] args) throws InterruptedException {
    
    
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();

        for (int i = 0; i < T; i++) {
    
    
            int meetingTimes = in.nextInt();

            List<Meeting> meetingList = new ArrayList<>();
            for (int j = 0; j < meetingTimes; j++) {
    
    
                int start = in.nextInt();
                int end = in.nextInt();
                meetingList.add(new Meeting(start,end));
            }
            if(meetingList.size()==1){
    
    
                System.out.println(meetingList.get(0).end-meetingList.get(0).start);
                continue;
            }
            findMaxDuration(meetingList);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_41866717/article/details/126537905