Skip to the code
cf.
+10 XP

#155 Slot for a New Timestamp

easy

An activity feed keeps event times in times, sorted from oldest to newest. Several events can share the same time. A new event with time time arrives, and it must be shown before any events that have the same time.

Write insertIndex(times, time) that returns the index where the new event should be inserted: the first index whose time is greater than or equal to time. If every time is smaller, return times.length.

The feed can be long, so aim for O(log n).

Example 1

Input: times = [100,200,200,300], time = 200
Output: 1

The first 200 is at index 1, so the new event goes there, before both copies.

Example 2

Input: times = [100,200,300], time = 250
Output: 2

300 at index 2 is the first time that is not smaller than 250.

Constraints

  • 0 <= times.length <= 100000
  • times is sorted ascending.
  • 0 <= times[i], time <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).