Skip to the code
cf.
+50 XP

#168 Wrapped Buffer With Repeats

hard

A sensor panel stores readings in a ring buffer. The readings were written in non-decreasing order and the buffer wrapped, so readings is a sorted list rotated by some amount. Unlike timestamps, readings can repeat, as in [4, 4, 5, 1, 2, 4, 4].

Write hasReading(readings, target) that returns true if target appears in readings and false otherwise.

It should be O(log n) when there are few repeats. When repeats hide which half is sorted, it may slow down, but it must stay correct.

Example 1

Input: readings = [4,4,5,1,2,4,4], target = 1
Output: true

1 sits right after the wrap.

Example 2

Input: readings = [4,4,5,1,2,4,4], target = 3
Output: false

No reading is 3.

Constraints

  • 1 <= readings.length <= 5000
  • readings is a non-decreasing array rotated by some amount.
  • -10^4 <= readings[i], target <= 10^4
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).