add monotonic queue algorithm#10531
Conversation
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
Please provide descriptive name for the parameter: k
There was a problem hiding this comment.
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.
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.
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.
Using the private _front is fairly risky. I.e. Not future-proof.
There was a problem hiding this comment.
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.
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.
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.
If it does not make sense then we can close the pull request.
There was a problem hiding this comment.
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.
@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: