forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoveCoveredIntervals.java
More file actions
41 lines (32 loc) · 861 Bytes
/
RemoveCoveredIntervals.java
File metadata and controls
41 lines (32 loc) · 861 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
32
33
34
35
36
37
38
39
40
41
//Author : Shobhit Behl (LC:shobhitbruh)
class Solution {
public int removeCoveredIntervals(int[][] arr) {
Arrays.sort(arr, (a, b) -> a[0]==b[0]?b[1]-a[1]:a[0]-b[0]);
int ans=0;
int j=-1;
for(int i=0;i<arr.length;i++){
if(arr[i][1]>j){
j=arr[i][1];
}else{
ans++;
}
}
return arr.length-ans;
}
class Solution {
// Tc: O(nlogn)
// Sc : O(1)
public int removeCoveredIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> (a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]));
int removalCount=0;
// Y coordinate
int prevRange = -1;
for(int[] interval : intervals){
if(prevRange>= interval[1]){
removalCount++;
}
prevRange = Math.max(interval[1], prevRange);
}
return intervals.length - removalCount;
}
}