[
array
bst
monotonic deque
heap
sliding window
]
BinarySearch 0955 Window Limits
Problem statement
https://binarysearch.com/problems/Window-Limits/
Solution
One solution is to use sortedlist, where we keep values of sliding window of size k.
Complexity
It is O(n log k) for time and O(k) for space.
Code
class Solution:
def solve(self, nums, k, t):
if k == 1: return True
SList = SortedList()
for i in range(len(nums)):
if i > k - 1: SList.remove(nums[i-k])
SList.add(nums[i])
if SList[-1] - SList[0] > t: return False
return True
Remark
Another solution is to use sliding min/max problem and check for each window if difference is big. Time complexity will be O(n), space is O(n) or O(k) depending on realization.