Skip to content

Instantly share code, notes, and snippets.

@lzdhlsc
Last active October 20, 2015 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzdhlsc/3d0896b9137c3109af3b to your computer and use it in GitHub Desktop.
Save lzdhlsc/3d0896b9137c3109af3b to your computer and use it in GitHub Desktop.
# collections.deque 如何?
# https://leetcode.com/problems/sliding-window-maximum/
dq = collections.deque() # dq keeps track of min(d[i] + a[i]) from i-1 to i-k
d[i] = dq[-1]
while len(dp) and dq[-1][0] >= d[i] + a[i]:
dq.pop()
dq.append((d[i] + a[i], i))
if dq[0][1] == i - k:
dp.popleft()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment