FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/IntervalListIntersections.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
IntervalListIntersections.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
120 lines (102 loc) · 3.27 KB
Breadcrumbs
coding-Interview
/
IntervalListIntersections.java
Copy path
File metadata and controls
120 lines (102 loc) · 3.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package
com
.
leetcode
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
PriorityQueue
;
public
class
IntervalListIntersections
{
//https://leetcode.com/problems/interval-list-intersections/
/*
Condition 1 :
|------------|
|------------|
|--------|
Condition 2
|------------|
|------------|
no overlap
Condition 3
|------------|
|------------------|
|------------|
Condition 4
|------------|
|--------|
|--------|
Condition 5 - Multiple overlap of second
|------------|
|---| |-------|
*/
//O(M+N) O(1) space
class
Solution
{
//O(M+N) O(1) space
public
int
[][]
intervalIntersection
(
int
[][]
A
,
int
[][]
B
) {
int
n
=
A
.
length
;
int
m
=
B
.
length
;
if
(
n
==
0
)
return
A
;
if
(
m
==
0
)
return
B
;
int
i
=
0
;
int
j
=
0
;
List
<
int
[]>
result
=
new
ArrayList
<>();
while
(
i
<
n
&&
j
<
m
){
int
[]
a
=
A
[
i
];
int
[]
b
=
B
[
j
];
//if overlap
if
(
isOverlap
(
a
,
b
)){
int
start
=
Math
.
max
(
a
[
0
],
b
[
0
]);
int
end
=
Math
.
min
(
a
[
1
],
b
[
1
]);
result
.
add
(
new
int
[]{
start
,
end
});
}
//done incrreas the one ending later
if
(
a
[
1
] <
b
[
1
]) {
i
++;
}
else
{
j
++;
}
}
int
[][]
resarr
=
new
int
[
result
.
size
()][
2
];
for
(
i
=
0
;
i
<
result
.
size
() ;
i
++){
resarr
[
i
] =
result
.
get
(
i
);
}
return
resarr
;
}
public
boolean
isOverlap
(
int
[]
a
,
int
[]
b
){
if
(
a
[
0
] >
b
[
1
] ||
b
[
0
] >
a
[
1
])
return
false
;
//condition 1 to 5
return
true
;
}
}
//Time O(MlogM + NlogN) Memory O(M+N)
class
SolutionMinHeap
{
// After adding all in minHeap with startTime sorted
public
int
[][]
intervalIntersection
(
int
[][]
A
,
int
[][]
B
) {
if
(
A
.
length
==
0
)
return
A
;
//empty
if
(
B
.
length
==
0
)
return
B
;
//empty
PriorityQueue
<
int
[]>
minHeap
=
new
PriorityQueue
<>((
a
,
b
) -> (
a
[
0
] -
b
[
0
]));
for
(
int
[]
a
:
A
){
minHeap
.
offer
(
a
);
}
for
(
int
[]
b
:
B
){
minHeap
.
offer
(
b
);
}
List
<
int
[]>
resultList
=
new
ArrayList
<>();
int
prev
[] =
minHeap
.
poll
();
while
(
minHeap
.
size
() >
0
){
int
[]
next
=
minHeap
.
poll
();
//find intersect only if it overlaps. Store overlap intersection
if
(
next
[
0
] <=
prev
[
1
]){
int
[]
r
=
new
int
[
2
];
r
[
0
] =
Math
.
max
(
next
[
0
],
prev
[
0
]);
r
[
1
] =
Math
.
min
(
next
[
1
],
prev
[
1
]);
resultList
.
add
(
r
);
}
// due to condition 5
if
(
prev
[
1
] >=
next
[
1
]){
continue
;
//stay previous as is
}
prev
=
next
;
}
int
[][]
result
=
new
int
[
resultList
.
size
()][
2
];
for
(
int
i
=
0
;
i
<
resultList
.
size
() ;
i
++){
result
[
i
] =
resultList
.
get
(
i
);
}
return
result
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL