-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
31 lines (30 loc) · 936 Bytes
/
Copy pathsolution.java
File metadata and controls
31 lines (30 loc) · 936 Bytes
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
// 56. Merge Intervals
// https://leetcode.com/problems/merge-intervals/
// Medium | Java | Accepted 2026-08-13
// Runtime 6 ms | Memory 48.7 MB
class Solution {
public int[][] merge(int[][] intervals) {
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
for(int[] i : intervals)
{
minHeap.add(i);
}
ArrayList<int[]> ans = new ArrayList<>();
while(!minHeap.isEmpty())
{
int[] interval = minHeap.poll();
while(!minHeap.isEmpty()&&minHeap.peek()[0]<=interval[1])
{
interval = new int[]{interval[0], Math.max(minHeap.peek()[1], interval[1])};
minHeap.poll();
}
ans.add(interval);
}
int[][] anss = new int[ans.size()][2];
for(int i = 0; i<ans.size(); i++)
{
anss[i] = ans.get(i);
}
return anss;
}
}