You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
for i inrange(len(nums)): # 移除不在窗口内的索引 if deq and deq[0] < i - k + 1: deq.popleft() # 移除小于当前元素的索引 while deq and nums[deq[-1]] < nums[i]: deq.pop() deq.append(i) # 从窗口的第一个完整位置开始,记录最大值 if i >= k - 1: result.append(nums[deq[0]])
Given two strings s and t of lengths m and n respectively, return _the minimum window substring_ofssuch that every character int( including duplicates ) is included in the window. If there is no such substring, return the empty string"".
The testcases will be generated such that the answer is unique .
Example 1:
1 2 3
Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
1 2 3
Input: s = "a", t = "a" Output: "a" Explanation: The entire string s is the minimum window.
Example 3:
1 2 3 4
Input: s = "a", t = "aa" Output: "" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
Constraints:
m == s.length
n == t.length
1 <= m, n <= 10^5
s and t consist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in O(m + n) time?
while right < len(s): # 即将移入窗口的字符 c = s[right] right += 1 # 更新窗口内的字符记录 if c in need: window[c] = window.get(c, 0) + 1 if window[c] == need[c]: # 当前字符的数量满足需求 valid += 1
# 判断左侧窗口是否需要收缩 while valid == len(need): # 更新最小覆盖子串 if right - left < length: start = left length = right - left
# d 是将移出窗口的字符 d = s[left] left += 1 # 更新窗口内的字符记录 if d in need: if window[d] == need[d]: # 当前字符数量不再满足需求 valid -= 1 window[d] -= 1# 减少窗口中该字符的数量