You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'''start[6:12]Problem Solving: [?] Sliding window algorithm * Potentially scan through with 2 pointers - left being the window shrinker - right being the window extender extend right point until meeting the condition of S contains all T take GlobalMin then shrink the size of left 2 pointer algorithm takes O(N), not sure yet for checking S contains all T part, I am a bit stuck here. Stuck: One way to verify Target in S, is the making a set of every window but that takes O(len(window)) time complexity for convertion Can we make a dictionary of S before hand "ADOBECODEBANC" { char : count A : 1 } I can think of a way in which having Target becomes a hashset Having a fixed hashtable to store S's state initially Having a second hashtable to change count frequency If visted, count of associated element decrement by 1 Each time, for loop -> Target hashset: then find comparision of inital hashtable & frequently changed hastable to get the diff If diff more than 1 for every element in hashset Meet the condition Time Complexity: O(N) + O(size of Target) [6:26] Ok, having brainfart , solution time? [7:06] Done, need to re-do'''fromcollectionsimportCounterclassSolution:
defminWindow(self, nums , target):
ifnumsisNone: return""targetHash=Counter(target)
curHash= {}
targetCount=len(targetHash)
matched=0globalMin=float("inf")
size=len(nums)
res=""j=0foriinrange(size):
# 不断增大窗口大小,知道满足所有Match需求whilej<sizeandmatched<targetCount:
ifnums[j] intargetHash:
curHash[nums[j]] =curHash.get(nums[j], 0) +1ifcurHash[nums[j]] ==targetHash[nums[j]]:
matched+=1j+=1# 更新最小值ifj-i<globalMinandmatched==targetCount:
globalMin=j-ires=nums[i:j]
# 删除left的一个元素,使得窗口继续滑动ifnums[i] intargetHash:
ifcurHash[nums[i]] ==targetHash[nums[i]]:
matched-=1curHash[nums[i]] -=1returnres
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: