[
binary search
sort
]
BinarySearch 0469 K Compare
Problem statement
https://binarysearch.com/problems/K-Compare/
Solution
Just sort b
and then use binary search. Or find number of elements, which are < sorted(b)[-k]
Complexity
It is O(m log m + n)
for time and O(m + n)
for space.
Code
class Solution:
def solve(self, a, b, k):
if not k: return len(a)
target = sorted(b)[-k]
return sum(x < target for x in a)