-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1272. Remove Interval.java
37 lines (30 loc) · 1.19 KB
/
1272. Remove Interval.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
// https://leetcode.com/problems/remove-interval/description/
class Solution {
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
TreeMap<Integer, Integer> line = new TreeMap<>();
for (int[] interval: intervals) {
line.put(interval[0], line.getOrDefault(interval[0], 0) + 1);
line.put(interval[1], line.getOrDefault(interval[1], 0) - 1);
}
line.put(toBeRemoved[0], line.getOrDefault(toBeRemoved[0], 0) - 1);
line.put(toBeRemoved[1], line.getOrDefault(toBeRemoved[1], 0) + 1);
int sum = 0;
boolean flag = false;
List<List<Integer>> result = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry: line.entrySet()) {
int point = entry.getKey();
int count = entry.getValue();
sum += count;
if (sum == 1) {
result.add(new ArrayList<>());
result.get(result.size() - 1).add(point);
flag = true;
}
if (sum == 0 && flag) {
result.get(result.size() - 1).add(point);
flag = false;
}
}
return result;
}
}