

用来验证的暴力解
public static class Program{public int start;public int end;public Program(int start, int end) {this.start = start;this.end = end;}}/*暴力解*/public static int bestArrange(Program[] programs) {if (programs == null || programs.length == 0) {return 0;}return process(programs, 0, 0);}/*目前来到timeLine的时间点,已经安排了done多的会议,剩下的会议programs可以自由安排*/private static int process(Program[] programs, int done, int timeLine) {if (programs.length == 0) {return done;}// 还有会议可以选择int max = done;// 当前安排的会议是什么会,每个都枚举for (int i = 0; i < programs.length; i++) {if (programs[i].start >= timeLine) {Program[] next = copyButExcept(programs, i);max = Math.max(max, process(next, done + 1, programs[i].end));}}return max;}private static Program[] copyButExcept(Program[] programs, int i) {Program[] ans = new Program[programs.length - 1];int index = 0;for (int k = 0; k < programs.length; k++) {if (k != i) {ans[index++] = programs[k];}}return ans;}
贪心
/*法二:按每场的结束时间*/public static int bestArrange2(Program[] programs) {Arrays.sort(programs, (o1, o2) -> o1.end - o2.end);int timeLine = 0;int result = 0;for (int i = 0; i < programs.length; i++) {if (programs[i].start >= timeLine) {result++;timeLine = programs[i].end;}}return result;}
