import
java.util.Arrays;
import
java.util.Comparator;
import
java.util.TreeSet;
class
Main
{
public
static
int
maximumShops(
int
[] opening,
int
[] closing,
int
n,
int
k)
{
Pair[] a =
new
Pair[n];
for
(
int
i =
0
; i < n; i++) {
a[i] =
new
Pair(opening[i], closing[i]);
}
Arrays.sort(a,
new
Comparator<Pair>() {
@Override
public
int
compare(Pair a, Pair b) {
if
(a.second != b.second) {
return
a.second - b.second;
}
return
a.first - b.first;
}
});
int
count =
0
;
TreeSet<Integer> st =
new
TreeSet<>();
for
(
int
i =
0
; i < n; i++)
{
boolean
flag =
false
;
if
(!st.isEmpty()) {
Integer it = st.higher(a[i].first);
if
(it !=
null
&& it <= a[i].first)
{
st.remove(it);
st.add(a[i].second);
count++;
flag =
true
;
}
}
if
(st.size() < k && flag ==
false
) {
st.add(a[i].second);
count++;
}
}
return
count+
1
;
}
public
static
void
main(String[] args)
{
int
S[] = {
1
,
8
,
3
,
2
,
6
};
int
E[] = {
5
,
10
,
6
,
5
,
9
};
int
K =
2
, N = S.length;
System.out.println(maximumShops(S, E, N, K));
}
static
class
Pair {
public
int
first;
public
int
second;
public
Pair(
int
first,
int
second) {
this
.first = first;
this
.second = second;
}
}
}