Range count with threshold
Given a static integer array and a list of [l, r, x] queries, return for each query the count of elements in the inclusive range arr[l..r] whose value is <= x. Build a merge-sort tree (a segment tree whose every node stores its segment's elements in sorted order); a query decomposes [l, r] into O(log n) nodes and binary-searches each sorted list for x, giving O(log^2 n) per query. Return the list of counts in query order.
Implement
range_count_leq(arr: list[int], queries: list[list[int]]) → list[int]Examples
in
[[1,5,2,8,3,7],[[0,5,4],[1,3,5],[2,2,2],[0,5,10]]]out[3,2,1,6]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 40 min
solution.py
InputExpectedGot
[[1,5,2,8,3,7],[[0,5,4],[1,3,5],[2,2,2],[0,5,10]]][3,2,1,6]not run yetsample