diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode406.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode406.java new file mode 100644 index 0000000..3a25cf8 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode406.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/7 21:23 + * @注释 + */ +public class LeetCode406 { + + @Test + public void test() + { + int[][] people = {{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}}; + for (int[] ints : new Solution().reconstructQueue(people)) { + System.out.println(Arrays.toString(ints)); + } + } + + + class Solution { + /** + * [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] + * @param people + * @return + */ + public int[][] reconstructQueue(int[][] people) { + List list = new ArrayList<>(); + Arrays.sort(people, (o1, o2) -> { + if (o1[0] == o2[0]) { + return o1[1] - o2[1]; + } else { + return o2[0] - o1[0]; + } + }); + + /** + * 遍历每一个,如果身高 + */ + for (int i = 0; i < people.length; i++) { + list.add(people[i][1], people[i]); + } + + return list.toArray(new int[people.length][2]); + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java new file mode 100644 index 0000000..128cdc5 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java @@ -0,0 +1,122 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/7 21:41 + * @注释 + */ +public class LeetCode435 { + + @Test + public void test() + { + So3 solution = new So3(); + // [0,2],[1,3],[2,4],[3,5],[4,6] + System.out.println(solution.eraseOverlapIntervals(new int[][]{{0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}})); + System.out.println(solution.eraseOverlapIntervals(new int[][]{{1, 2}, {1, 2}, {1, 2}})); + System.out.println(solution.eraseOverlapIntervals(new int[][]{{1, 2}, {2, 3}})); + } + + class Solution { + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + if (o1[0] == o2[0]) { + return o1[1] - o2[1]; + } + return o1[0] - o2[0]; + } + }); + +// int start = intervals[0][0]; + int end = intervals[0][1]; + int res = 0; + for (int i = 1; i < intervals.length; i++) { + int[] next = intervals[i]; + /** + * | | + * | | + * + * | | + * | | + * + * | | + * | | + * + */ + int nextStart = next[0]; + int nextEnd = next[1]; + if (end <= nextStart) { +// start = nextStart; + end = nextEnd; + }else if (nextEnd <= end) { + res++; + end = nextEnd; + } else { + res++; + } + } + return res; + } + } + + class Solution2 { + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o1[1] - o2[1]; + } + }); + int end = intervals[0][1]; + int can = 1; + for (int i = 1; i < intervals.length; i++) { + int[] next = intervals[i]; + int nextStart = next[0]; + int nextEnd = next[1]; + // 如果上次活动结束比下次开始时间早,能够多参加一次 + if (nextStart >= end) { + can++; + end = nextEnd; + } + } + + return intervals.length - can; + + } + } + + class So3{ + public int eraseOverlapIntervals(int[][] intervals) { + // 最多能参加几次活动 + Arrays.sort(intervals, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o1[0] - o2[0]; + } + }); + + int participation = 1; + int end = intervals[0][1]; + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] < end) { + // 后一个活动的开始时间早于头一个活动开始时间,这个活动不能参加 + end = Math.min(end, intervals[i][1]); + } else { + participation++; + end = intervals[i][1]; + } + } + + return intervals.length - participation; + + } + } +}