-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path56.merge-intervals.java
45 lines (35 loc) · 1.33 KB
/
56.merge-intervals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* @lc app=leetcode id=56 lang=java
*
* [56] Merge Intervals
*/
// @lc code=start
class Solution {
public int[][] merge(int[][] intervals) {
Collections.sort(Arrays.asList(intervals), new IntervalComparator());
List<int[]> result = new ArrayList<>();
for (int[] interval : intervals) {
if (result.isEmpty() || result.get(result.size() - 1)[1] < interval[0]) {
result.add(interval);
} else {
int tempMax = Math.max(result.get(result.size() - 1)[1], interval[1]);
result.get(result.size() - 1)[1] = tempMax;
}
}
return result.toArray(new int[result.size()][2]);
}
private class IntervalComparator implements Comparator<int[]> {
@Override
public int compare(int[] a1, int[] a2) {
return a1[0] - a2[0];
}
}
}
/*
Facebook Follow-Up
Question: How do you add intervals and merge them for a large stream of intervals? (Facebook Follow-up Question)
Method 1: Inspired by https://leetcode.com/problems/merge-intervals/discuss/21452/Share-my-interval-tree-solution-no-sorting
We need to have two functions for the tree [add interval and query tree]. (https://leetcode.com/problems/merge-intervals/solution/)
Method 2: Use heap to control the stream. (Personal Idea).
*/
// @lc code=end