Skip to content

Commit dbe82c0

Browse files
Merge branch 'master' into fix/binary-search-duplicates-13840
2 parents de9d3b2 + 678dedb commit dbe82c0

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ repos:
1414
- id: requirements-txt-fixer
1515

1616
- repo: https://github.com/MarcoGorelli/auto-walrus
17-
rev: 0.3.4
17+
rev: 0.4.1
1818
hooks:
1919
- id: auto-walrus
2020

2121
- repo: https://github.com/astral-sh/ruff-pre-commit
22-
rev: v0.14.10
22+
rev: v0.14.14
2323
hooks:
2424
- id: ruff-check
2525
- id: ruff-format
@@ -32,7 +32,7 @@ repos:
3232
- tomli
3333

3434
- repo: https://github.com/tox-dev/pyproject-fmt
35-
rev: v2.11.1
35+
rev: v2.12.1
3636
hooks:
3737
- id: pyproject-fmt
3838

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@
881881
* [Quine](other/quine.py)
882882
* [Scoring Algorithm](other/scoring_algorithm.py)
883883
* [Sdes](other/sdes.py)
884+
* [Sliding Window Maximum](other/sliding_window_maximum.py)
884885
* [Tower Of Hanoi](other/tower_of_hanoi.py)
885886
* [Word Search](other/word_search.py)
886887

ciphers/caesar_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
4545
And our shift is ``2``
4646
4747
We can then encode the message, one letter at a time. ``H`` would become ``J``,
48-
since ``J`` is two letters away, and so on. If the shift is ever two large, or
48+
since ``J`` is two letters away, and so on. If the shift is ever too large, or
4949
our letter is at the end of the alphabet, we just start at the beginning
5050
(``Z`` would shift to ``a`` then ``b`` and so on).
5151

other/sliding_window_maximum.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from collections import deque
2+
3+
4+
def sliding_window_maximum(numbers: list[int], window_size: int) -> list[int]:
5+
"""
6+
Return a list containing the maximum of each sliding window of size window_size.
7+
8+
This implementation uses a monotonic deque to achieve O(n) time complexity.
9+
10+
Args:
11+
numbers: List of integers representing the input array.
12+
window_size: Size of the sliding window (must be positive).
13+
14+
Returns:
15+
List of maximum values for each valid window.
16+
17+
Raises:
18+
ValueError: If window_size is not a positive integer.
19+
20+
Time Complexity: O(n) - each element is added and removed at most once
21+
Space Complexity: O(k) - deque stores at most window_size indices
22+
23+
Examples:
24+
>>> sliding_window_maximum([1, 3, -1, -3, 5, 3, 6, 7], 3)
25+
[3, 3, 5, 5, 6, 7]
26+
>>> sliding_window_maximum([9, 11], 2)
27+
[11]
28+
>>> sliding_window_maximum([], 3)
29+
[]
30+
>>> sliding_window_maximum([4, 2, 12, 3], 1)
31+
[4, 2, 12, 3]
32+
>>> sliding_window_maximum([1], 1)
33+
[1]
34+
"""
35+
if window_size <= 0:
36+
raise ValueError("Window size must be a positive integer")
37+
if not numbers:
38+
return []
39+
40+
result: list[int] = []
41+
index_deque: deque[int] = deque()
42+
43+
for current_index, current_value in enumerate(numbers):
44+
# Remove the element which is out of this window
45+
if index_deque and index_deque[0] == current_index - window_size:
46+
index_deque.popleft()
47+
48+
# Remove useless elements (smaller than current) from back
49+
while index_deque and numbers[index_deque[-1]] < current_value:
50+
index_deque.pop()
51+
52+
index_deque.append(current_index)
53+
54+
# Start adding to result once we have a full window
55+
if current_index >= window_size - 1:
56+
result.append(numbers[index_deque[0]])
57+
58+
return result

strings/reverse_words.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
def reverse_words(input_str: str) -> str:
2-
"""
3-
Reverses words in a given string
1+
def reverse_words(sentence: str) -> str:
2+
"""Reverse the order of words in a given string.
3+
4+
Extra whitespace between words is ignored.
5+
46
>>> reverse_words("I love Python")
57
'Python love I'
68
>>> reverse_words("I Love Python")
79
'Python Love I'
810
"""
9-
return " ".join(input_str.split()[::-1])
11+
return " ".join(sentence.split()[::-1])
1012

1113

1214
if __name__ == "__main__":

0 commit comments

Comments
 (0)