class Solution:
def shortestSeq(self, big: List[int], small: List[int]) -> List[int]:
value = N_small = len(small)
N_big = len(big)
dic = dict()
for num in small:
if num not in dic:
dic[num] = 0
dic[num] += 1
l = 0
r = 0
minLength = float('inf')
ans = [-1,-1]
while r < N_big:
if big[r] in dic:
dic[big[r]] -= 1
if dic[big[r]] >= 0:
value -= 1
while value == 0:
if r-l+1 < minLength:
ans[0] = l
ans[1] = r
minLength = r-l+1
leftNum = big[l]
if leftNum in dic:
dic[leftNum] += 1
if dic[leftNum] > 0:
value += 1
l += 1
r += 1
if ans[0]==-1 and ans[1]==-1:
return []
else:
return ans
每日一道Leetcode - 面试题 17.18. 最短超串 [滑动窗口]
最新推荐文章于 2021-12-27 19:31:00 发布