Skip to content

Commit

Permalink
update 56 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 3, 2024
1 parent b27fa64 commit 8aa5ee5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 9 additions & 5 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
65 changes: 38 additions & 27 deletions leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<int[]> 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<int[]> merged = new LinkedList<>();
for (int[] interval : intervals) {
// if the list of merged intervals is empty or if the current
Expand Down

0 comments on commit 8aa5ee5

Please sign in to comment.