[
intervals
sort
]
BinarySearch 0567 Contained Interval
Problem statement
https://binarysearch.com/problems/Contained-Interval/
Solution
The ides is to sort intervals by starts and then for ends check if we have some end smaller or equal then next end. Also if we have the same start we sort ends by decrease (or we can return true immedietly).
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, I):
I = sorted(I, key = lambda x: (x[0], -x[1]))
for i in range(1, len(I)):
if I[i][1] <= I[i - 1][1]: return True
return False