Skip to content

Commit

Permalink
add 056 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 11, 2023
1 parent 6a6a611 commit 475d374
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [Python](./leetcode_python/Array/add-to-array-form-of-integer.py) | _O(n)_ | _O(1)_ | Easy |add xxx to sum, fb| AGAIN (not start)
004 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Python](./leetcode_python/Array/median-of-two-sorted-arrays.py) | _O(n)_ | _O(1)_ | Hard |array, heapq, binary search, amazon| AGAIN* (1)
010 | [Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/) | [Python](./leetcode_python/Array/regular-expression-matching.py) | _O(n)_ | _O(1)_ | Hard |recursion, dp, `fb`| AGAIN (not start)
056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [Python](./leetcode_python/Array/merge-intervals.py) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, `good trick`,057 Insert Interval, `twitter`,`M$`,`UBER`,`google`,`amazon`,`fb`| OK********* (6) (but AGAIN)
056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [Python](./leetcode_python/Array/merge-intervals.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, `good trick`,057 Insert Interval, `twitter`,`M$`,`UBER`,`google`,`amazon`,`fb`| OK********* (6) (but AGAIN)
759 | [Employee Free Time](https://leetcode.com/problems/employee-free-time/) | [Python](./leetcode_python/Array/employee-free-time.py) | _O(n)_ | _O(1)_ | Hard |LC 56 and LC 986, heap, merger intervals, scanning line, `amazon`, fb, google, uber, airbnb| AGAIN********** (2) (not start)
1014 | [Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | [Python](./leetcode_python/Array/best_sightseeing_pair.py) | | | Medium |dp, array, good basic, Spotify| AGAIN (not start)
1027 | [Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/) | [Python](./leetcode_python/Array/longest-arithmetic-subsequence.py) | | | Medium |dp, hash table, trick, google, amazon| AGAIN*** (not start)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20231211: 39,53,48
20231211: 39,53,48,56
20231208: 33
20231206: 19,003
20231205: 23
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
2024-02-04 -> ['39,53,48']
2024-02-04 -> ['39,53,48,56']
2024-01-30 -> ['19,003']
2024-01-29 -> ['23']
2024-01-28 -> ['55,45']
2024-01-14 -> ['39,53,48']
2024-01-14 -> ['39,53,48,56']
2024-01-09 -> ['19,003']
2024-01-08 -> ['23']
2024-01-07 -> ['55,45']
2024-01-02 -> ['452,406,135']
2024-01-01 -> ['39,53,48']
2024-01-01 -> ['39,53,48,56']
2023-12-31 -> ['134']
2023-12-28 -> ['53']
2023-12-27 -> ['19,003']
2023-12-26 -> ['23']
2023-12-25 -> ['55,45']
2023-12-24 -> ['39,53,48']
2023-12-19 -> ['39,53,48', '19,003']
2023-12-24 -> ['39,53,48,56']
2023-12-19 -> ['39,53,48,56', '19,003']
2023-12-18 -> ['23']
2023-12-17 -> ['55,45']
2023-12-16 -> ['39,53,48']
2023-12-14 -> ['39,53,48', '19,003']
2023-12-13 -> ['39,53,48', '23']
2023-12-12 -> ['39,53,48', '55,45', '452,406,135']
2023-12-16 -> ['39,53,48,56']
2023-12-14 -> ['39,53,48,56', '19,003']
2023-12-13 -> ['39,53,48,56', '23']
2023-12-12 -> ['39,53,48,56', '55,45', '452,406,135']
2023-12-11 -> ['19,003']
2023-12-10 -> ['23', '134']
2023-12-09 -> ['19,003', '55,45']
Expand Down
174 changes: 174 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/merge-intervals/

import java.util.*;

public class MergeIntervals {

/**
* Exp 1:
* input = [[1,3],[2,6],[8,10],[15,18]]
*
* Step 1) : sort (1st element small -> big, then 2nd element, small -> big)
*
* [[1,3],[2,6],[8,10],[15,18]]
*
* Step 2) : merge interval
*
* [[1,3]]
*
* [[1,6]]
*
* [[1,6], [8,10]]
*
* [[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;
// }

// 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]));
LinkedList<int[]> merged = 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 (merged.isEmpty() || merged.getLast()[1] < interval[0]) {
merged.add(interval);
}
// otherwise, there is overlap, so we merge the current and previous
// intervals.
else {
merged.getLast()[1] = Math.max(merged.getLast()[1], interval[1]);
}
}
return merged.toArray(new int[merged.size()][]);
}

// V2
// IDEA : Connected Components
// https://leetcode.com/problems/merge-intervals/editorial/
private Map<int[], List<int[]>> graph;
private Map<Integer, List<int[]>> nodesInComp;
private Set<int[]> visited;

// return whether two intervals overlap (inclusive)
private boolean overlap(int[] a, int[] b) {
return a[0] <= b[1] && b[0] <= a[1];
}

// build a graph where an undirected edge between intervals u and v exists
// iff u and v overlap.
private void buildGraph(int[][] intervals) {
graph = new HashMap<>();
for (int[] interval : intervals) {
graph.put(interval, new LinkedList<>());
}

for (int[] interval1 : intervals) {
for (int[] interval2 : intervals) {
if (overlap(interval1, interval2)) {
graph.get(interval1).add(interval2);
graph.get(interval2).add(interval1);
}
}
}
}

// merges all of the nodes in this connected component into one interval.
private int[] mergeNodes(List<int[]> nodes) {
int minStart = nodes.get(0)[0];
for (int[] node : nodes) {
minStart = Math.min(minStart, node[0]);
}

int maxEnd = nodes.get(0)[1];
for (int[] node : nodes) {
maxEnd = Math.max(maxEnd, node[1]);
}

return new int[] {minStart, maxEnd};
}

// use depth-first search to mark all nodes in the same connected component
// with the same integer.
private void markComponentDFS(int[] start, int compNumber) {
Stack<int[]> stack = new Stack<>();
stack.add(start);

while (!stack.isEmpty()) {
int[] node = stack.pop();
if (!visited.contains(node)) {
visited.add(node);

if (nodesInComp.get(compNumber) == null) {
nodesInComp.put(compNumber, new LinkedList<>());
}
nodesInComp.get(compNumber).add(node);

for (int[] child : graph.get(node)) {
stack.add(child);
}
}
}
}

// gets the connected components of the interval overlap graph.
private void buildComponents(int[][] intervals) {
nodesInComp = new HashMap<>();
visited = new HashSet<>();
int compNumber = 0;

for (int[] interval : intervals) {
if (!visited.contains(interval)) {
markComponentDFS(interval, compNumber);
compNumber++;
}
}
}

public int[][] merge_2(int[][] intervals) {
buildGraph(intervals);
buildComponents(intervals);

// for each component, merge all intervals into one interval.
List<int[]> merged = new LinkedList<>();
for (int comp = 0; comp < nodesInComp.size(); comp++) {
merged.add(mergeNodes(nodesInComp.get(comp)));
}

return merged.toArray(new int[merged.size()][]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void _reverse(int[] input){
l += 1;
r -= 1;
}

}

// V1
Expand Down

0 comments on commit 475d374

Please sign in to comment.