Skip to content

Commit dcd35f2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 35a2ed7 commit dcd35f2

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

data_structures/sliding_window/longest_substring_two_distinct.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,29 @@ def length_of_longest_substring_two_distinct(s):
2222
n = len(s)
2323
if n == 0:
2424
return 0
25-
25+
2626
# Dictionary to store the last occurrence of each character
2727
char_map = {}
2828
left = 0
2929
max_length = 0
30-
30+
3131
# Sliding window approach
3232
for right in range(n):
3333
char_map[s[right]] = right
34-
34+
3535
# If we have more than two distinct characters
3636
if len(char_map) > 2:
3737
# Remove the leftmost character
3838
del_idx = min(char_map.values())
3939
del char_map[s[del_idx]]
4040
left = del_idx + 1
41-
41+
4242
max_length = max(max_length, right - left + 1)
43-
43+
4444
return max_length
4545

46+
4647
if __name__ == "__main__":
4748
import doctest
49+
4850
doctest.testmod()

data_structures/sliding_window/max_sum_subarray.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ def max_sum_subarray(arr, k):
2323
if n < k or k <= 0:
2424
print("Invalid input: k is larger than the array size or non-positive")
2525
return None
26-
26+
2727
# Calculate the sum of the first window of size k
2828
window_sum = sum(arr[:k])
2929
max_sum = window_sum
30-
30+
3131
# Slide the window from start to end of the array
3232
for i in range(n - k):
3333
window_sum = window_sum - arr[i] + arr[i + k]
3434
max_sum = max(max_sum, window_sum)
35-
35+
3636
return max_sum
3737

38+
3839
if __name__ == "__main__":
3940
import doctest
41+
4042
doctest.testmod()

data_structures/sliding_window/min_subarray_len.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ def min_subarray_len(target, nums):
2323
n = len(nums)
2424
left = 0
2525
current_sum = 0
26-
min_length = float('inf')
27-
26+
min_length = float("inf")
27+
2828
for right in range(n):
2929
current_sum += nums[right]
30-
30+
3131
while current_sum >= target:
3232
min_length = min(min_length, right - left + 1)
3333
current_sum -= nums[left]
3434
left += 1
35-
36-
return min_length if min_length != float('inf') else 0
35+
36+
return min_length if min_length != float("inf") else 0
37+
3738

3839
if __name__ == "__main__":
3940
import doctest
41+
4042
doctest.testmod()

0 commit comments

Comments
 (0)