-
-
Notifications
You must be signed in to change notification settings - Fork 46.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add monotonic queue algorithm #10531
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
expect = [3, 3, 5, 5, 6, 7] | ||
|
||
|
||
def max_sliding_window(arr: list[float], k: int) -> list[float]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: k
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to window_size
@@ -0,0 +1,39 @@ | |||
from __future__ import annotations | |||
|
|||
from .double_ended_queue import Deque |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this use https://docs.python.org/3/library/collections.html#collections.deque or is there a special capability in the local version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can. And so that we can avoid using private attributes
queue = Deque() | ||
for i in range(len(arr)): | ||
# pop the element if the index is outside the window size k | ||
if queue and i - queue._front.val >= window_size: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the private _front
is fairly risky. I.e. Not future-proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I use the collections.deque now
expect = [3, 3, 5, 5, 6, 7] | ||
|
||
|
||
def max_sliding_window(arr: list[float], window_size: int) -> list[float]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we take the same approach with this algorithm?
#10273 (comment)
Seems appropriate for a sliding_window algorithm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit different because the queue has to be monotonically decreasing while calculating the max value in a window.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it does not make sense then we can close the pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I resolved the issue of using collections.deque instead of double_ended_queue.
I feel the max sliding window is still an important algorithm which is a good use case of the queue (it is similar to next_greater_element in stack).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tianyizheng02 Is my iterator, not list request above unreasonable in this algorithm?
Describe your change:
Similar the monotonic stack implementation in next_greater_element.py, I implemented an epic monotonical queue example: max_sliding_window which is to continuously generate max value in a sliding window over an array.
I also implemented a test with one example and the test has passed.
Checklist: