TikTok OA 面试真题解析:Nearby Duplicate Episode Check|数组滑动窗口

5次阅读
没有评论

Given an integer array v, where v[i] represents the episode watched on day i, and two integers k and t, determine whether there exist two different days i and j such that:

  • Day difference: abs(i - j) < k
  • Episode difference: abs(v[i] - v[j]) < t

Return True if both conditions are satisfied; otherwise, return False.

这道题本质上是在判断数组中是否存在一对“足够近”的元素,同时它们的数值差也要足够小。常见做法是结合滑动窗口和有序结构(如平衡树、桶分组或 TreeSet 思路):窗口维护最近 <code>k</code> 天内的值,逐个检查当前值附近是否存在与之差值小于 <code>t</code> 的元素;如果窗口超过 <code>k</code>,就移除最旧元素。这样可以把暴力的双重循环优化到接近 <code>O(n log k)</code> 或 <code>O(n)</code>,非常适合在线笔试中的数组查找类题目。

正文完
 0