From 8aa5ee5ab246a70286dede05d0fd941b4c349c62 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 3 Mar 2024 17:41:29 +0800 Subject: [PATCH] update 56 java, update progress --- data/progress.txt | 1 + data/to_review.txt | 14 ++-- .../LeetCodeJava/Array/MergeIntervals.java | 65 +++++++++++-------- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 2f811679..f7182ce2 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240303: 55(again),56 20240229: 39,48(again),49,53,54 20240228: 20,21,23,33(again) 20240227: 1,3,5,4,19 diff --git a/data/to_review.txt b/data/to_review.txt index 1aca73a9..1de1f7b1 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,20 +1,24 @@ +2024-04-27 -> ['55(again),56'] 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] +2024-04-06 -> ['55(again),56'] 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] +2024-03-24 -> ['55(again),56'] 2024-03-21 -> ['39,48(again),49,53,54'] 2024-03-20 -> ['20,21,23,33(again)'] 2024-03-19 -> ['1,3,5,4,19'] +2024-03-16 -> ['55(again),56'] 2024-03-13 -> ['39,48(again),49,53,54'] 2024-03-12 -> ['20,21,23,33(again)'] -2024-03-11 -> ['1,3,5,4,19'] -2024-03-08 -> ['39,48(again),49,53,54'] +2024-03-11 -> ['55(again),56', '1,3,5,4,19'] +2024-03-08 -> ['55(again),56', '39,48(again),49,53,54'] 2024-03-07 -> ['20,21,23,33(again)'] -2024-03-06 -> ['1,3,5,4,19'] -2024-03-05 -> ['39,48(again),49,53,54'] -2024-03-04 -> ['20,21,23,33(again)'] +2024-03-06 -> ['55(again),56', '1,3,5,4,19'] +2024-03-05 -> ['55(again),56', '39,48(again),49,53,54'] +2024-03-04 -> ['55(again),56', '20,21,23,33(again)'] 2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19'] 2024-03-02 -> ['39,48(again),49,53,54', '20,21,23,33(again)'] 2024-03-01 -> ['39,48(again),49,53,54', '20,21,23,33(again)', '1,3,5,4,19'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java index 3dc3b024..98f54260 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java @@ -25,41 +25,52 @@ public class MergeIntervals { * [[1,6], [8,10], [15,18]] */ - // TODO : fix V0 // V0 // IDEA : ARRAY OP + BOUNDARY OP -// public int[][] merge(int[][] intervals) { -// -// if (intervals == null || intervals.length == 0){ -// return intervals; -// } -// -// // sort -//// Arrays.stream(intervals).sorted(); -//// -//// Arrays.stream(intervals).sorted((x,y) -> { -//// // Step 1) compare first element -//// if (x[0] > 0) { -//// return 1; -//// } else if (x[0] > 0) { -//// return -1; -//// } -//// -//// // Step 2) compare second element -//// -//// return 0; -//// }); -// -// int[] tmp = new int[]{}; -// -// return intervals; -// } + public int[][] merge(int[][] intervals) { + /** + * + * + * 1) Arrays.sort(intervals, ...) is used to sort the intervals array. + * + * 2) (a, b) -> Integer.compare(a[0], b[0]) is a lambda expression used as a comparator for sorting. + * + * 3) a and b are two intervals (arrays) being compared. + * a[0] and b[0] are the first elements of the intervals, which represent the start values of the intervals. + * Integer.compare(a[0], b[0]) + * compares the start values of intervals a and b and + * returns -1 if a should come before b, + * 0 if they are equal, + * and 1 if b should come before a. + * + * -> Putting it all together, the Arrays.sort method uses the provided comparator (a, b) -> Integer.compare(a[0], b[0]) to sort the intervals array based on the start values of the intervals. + * + */ + // NOTE !!! sort on 1st element + Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); + // NOTE !!! we set res as linkedlist type (can use queue as well) + LinkedList res = new LinkedList<>(); + for (int[] interval : intervals) { + // if the list of merged intervals is empty or if the current + // interval does not overlap with the previous, simply append it. + if (res.isEmpty() || res.getLast()[1] < interval[0]) { + res.add(interval); + } + // otherwise, there is overlap, so we merge the current and previous + // intervals. + else { + res.getLast()[1] = Math.max(res.getLast()[1], interval[1]); + } + } + return res.toArray(new int[res.size()][]); + } // V1 // IDEA : Sorting // https://leetcode.com/problems/merge-intervals/editorial/ public int[][] merge_1(int[][] intervals) { Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); + // NOTE !!! we set res as linkedlist type LinkedList merged = new LinkedList<>(); for (int[] interval : intervals) { // if the list of merged intervals is empty or if the current