[
intervals
greedy
binary search
sort
line sweep
]
BinarySearch 0961 Kth User to Visit Website
Problem statement
https://binarysearch.com/problems/Kth-User-to-Visit-Website/
Solution
Notice, that k
-th request can not happen earlier than k
-th smallest start of requests. With similar logic it can not happen later than k
-th smallest end of requests. Let us find these two values and then find all indexes of persons with overlapping with given interval.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, requests, k):
ks = sorted(s for s, e in requests)[k]
ke = sorted(e for s, e in requests)[k]
return [i for i, (s, e) in enumerate(requests) if ke >= s and e >= ks]