Problem 201847 · easy · Phase 02 Linear Data Structures

Nearby Duplicate Reading

hash maps · index map

A sensor produces readings. A reading is a glitch if the same value appears twice within k positions: there exist indices i < j with readings[i] == readings[j] and j - i <= k. Return True if a glitch exists, otherwise False.

Examples

Input:  readings = [7, 3, 7], k = 2
Output: True

Input:  readings = [7, 3, 7], k = 1
Output: False

Constraints

  • 0 <= len(readings) <= 10**5, 0 <= k <= 10**5
  • Target complexity: O(n) time.

Goals

  • Store the most recent index of each value
  • Check a distance condition in one pass
Starting Python…