Problem 341536 · medium · Level 03 Linear Management & Searching

Value at a Timestamp

bisect module · binary search

A sensor logged readings: times is a strictly increasing list of timestamps and values[i] is the reading taken at times[i]. For every query time t in queries, report the reading with the largest timestamp <= t, or None if no reading is that old. Return the list of answers in query order.

Examples

Input:  times = [1, 5, 9], values = ["a", "b", "c"], queries = [5, 6, 0, 100]
Output: ["b", "b", None, "c"]

Constraints

  • 0 <= len(times) == len(values) <= 10**5, 0 <= len(queries) <= 10**5
  • Required time: O(q log n). A linear scan per query is too slow.

Goals

  • Use bisect_right to find the latest entry not after a query time
  • Answer many queries against one sorted key list
Starting Python…