From 8b775ad43946e1df82acd619a9b415e18b09cda3 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:31:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(redo):=20=E6=B7=BB=E5=8A=A0LeetCode?= =?UTF-8?q?=E9=97=AE=E9=A2=98134=E3=80=81135=E3=80=81860=E3=80=811005?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=9B=9B=E4=B8=AAJava=E7=B1=BB=EF=BC=8C=E5=88=86=E5=88=AB?= =?UTF-8?q?=E5=AF=B9=E5=BA=94LeetCode=E9=97=AE=E9=A2=98134=E3=80=81135?= =?UTF-8?q?=E3=80=81860=E3=80=811005=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=E3=80=82=20=E8=BF=99=E4=BA=9B=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=B6=89=E5=8F=8A=E5=BE=AA=E7=8E=AF=E6=95=B0=E7=BB=84=E3=80=81?= =?UTF-8?q?=E7=B3=96=E6=9E=9C=E5=88=86=E9=85=8D=E3=80=81=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2=E5=92=8C=E6=95=B0=E7=BB=84=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=AD=89=E4=B8=8D=E5=90=8C=E4=B8=BB=E9=A2=98=E3=80=82=20?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E7=B1=BB=E5=8C=85=E5=90=AB=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=88=96=E5=A4=9A=E4=B8=AA=E8=A7=A3=E5=86=B3=E7=89=B9=E5=AE=9A?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=9A=84=E7=AE=97=E6=B3=95=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E5=8F=8A=E7=9B=B8=E5=85=B3=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LeetCode134:实现计算加油站问题的解决方案。 - LeetCode135:实现计算糖果分配问题的解决方案。 - LeetCode860:实现柠檬水找零问题的解决方案。 - LeetCode1005:实现数组操作以最大化和的解决方案。 接下来的步骤包括将这些解决方案集成到主项目中,并通过单元测试确保其正确性。 --- .../redo/redo_all_240721/LeetCode406.java | 54 ++++++++ .../redo/redo_all_240721/LeetCode435.java | 122 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode406.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode435.java 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; + + } + } +}