Python violence Clearance
1 classsolution (object):2 defMaxslidingwindow (self, nums, K):3 """4 : Type Nums:list[int]5 : Type K:int6 : Rtype:list[int]7 """8RET = []9Length =Len (nums)Ten if0 = =Length: One return [] Aw = length-k + 1 - forIinchRange (W): -m = Max (nums[i:i+K]) the ret.append (m) - returnRet
JavaScript bidirectional queue complexity O (n)
1 varMaxslidingwindow =function(Nums, k) {2 //constructs a two-way queue3 varwindow = [];4 5 varAnswer = [];6 for(vari = 0; i < nums.length; i++) {7 8 //Delete an index outside the window9 if(0! = window.length && window[0] = = i-k)Ten Window.shift (); One A //The index in the window that is smaller than the index at the time of each newly added index - //Is never going to work in the next window move. - //which implies that the index values in the window are ordered. the - //Delete the invalid index in the window in turn - while(0! = Window.length && Nums[window[window.length-1]] <Nums[i]) - Window.pop (); + - Window.push (i); + if(I >= k-1) AAnswer.push (nums[window[0]]); at } - returnanswer; -};
239. Sliding Window Maximum