Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions data_structures/arrays/fruit_into_baskets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Question:
Given an array fruits representing types of fruit, pick a contiguous subarray containing at most 2 different types of fruit.

Check failure on line 3 in data_structures/arrays/fruit_into_baskets.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/fruit_into_baskets.py:3:89: E501 Line too long (124 > 88)
Return the maximum number of fruits you can collect.

Example:
Input: fruits = [1,2,1,2,3]
Output: 4
Explanation:
Pick subarray [1,2,1,2] -> contains 2 types and length 4
"""

from typing import List

Check failure on line 13 in data_structures/arrays/fruit_into_baskets.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/fruit_into_baskets.py:13:1: UP035 `typing.List` is deprecated, use `list` instead
from collections import defaultdict

Check failure on line 14 in data_structures/arrays/fruit_into_baskets.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/arrays/fruit_into_baskets.py:13:1: I001 Import block is un-sorted or un-formatted

class FruitIntoBaskets:
def total_fruit(self, fruits: List[int]) -> int:

Check failure on line 17 in data_structures/arrays/fruit_into_baskets.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/fruit_into_baskets.py:17:35: UP006 Use `list` instead of `List` for type annotation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/fruit_into_baskets.py, please provide doctest for the function total_fruit

count = defaultdict(int) # Stores count of each fruit type
left = 0 # Left pointer of sliding window
max_fruit = 0 # Tracks max number of fruits collected

for right, fruit in enumerate(fruits):
count[fruit] += 1 # Include current fruit

# Shrink window if more than 2 types
while len(count) > 2:
count[fruits[left]] -= 1
if count[fruits[left]] == 0:
del count[fruits[left]]
left += 1

max_fruit = max(max_fruit, right - left + 1)

return max_fruit

# Example dry run
if __name__ == "__main__":
fruits = [1,2,1,2,3]
solver = FruitIntoBaskets()
print("Maximum Fruits Collected:", solver.total_fruit(fruits))
39 changes: 39 additions & 0 deletions data_structures/arrays/longest_ones_after_replacement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Question:
Given a binary array nums and an integer k, find the length of the longest subarray containing 1s after flipping at most k zeros.

Check failure on line 3 in data_structures/arrays/longest_ones_after_replacement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/longest_ones_after_replacement.py:3:89: E501 Line too long (129 > 88)

Example:
Input: nums = [1,0,1,1,0,1], k = 1
Output: 4
Explanation:
Flip the first 0 at index 1 -> subarray [1,1,1,1] has length 4
"""

from typing import List

Check failure on line 12 in data_structures/arrays/longest_ones_after_replacement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/arrays/longest_ones_after_replacement.py:12:1: I001 Import block is un-sorted or un-formatted

Check failure on line 12 in data_structures/arrays/longest_ones_after_replacement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/longest_ones_after_replacement.py:12:1: UP035 `typing.List` is deprecated, use `list` instead

class LongestOnesAfterReplacement:
def longest_ones(self, nums: List[int], k: int) -> int:

Check failure on line 15 in data_structures/arrays/longest_ones_after_replacement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/longest_ones_after_replacement.py:15:34: UP006 Use `list` instead of `List` for type annotation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/longest_ones_after_replacement.py, please provide doctest for the function longest_ones

Please provide descriptive name for the parameter: k

left = 0 # Left pointer of sliding window
max_len = 0 # Tracks maximum window length
zeros_count = 0 # Count of zeros in current window

for right in range(len(nums)):
if nums[right] == 0:
zeros_count += 1

# Shrink window if zeros exceed k
while zeros_count > k:
if nums[left] == 0:
zeros_count -= 1
left += 1

max_len = max(max_len, right - left + 1)

return max_len

# Example dry run
if __name__ == "__main__":
nums = [1,0,1,1,0,1]
k = 1
solver = LongestOnesAfterReplacement()
print("Longest Ones After Replacement:", solver.longest_ones(nums, k))
51 changes: 51 additions & 0 deletions data_structures/arrays/sliding_window_maximum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Question:
Given an integer array nums and an integer k, return the maximum value in each sliding window of size k.

Check failure on line 3 in data_structures/arrays/sliding_window_maximum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window_maximum.py:3:89: E501 Line too long (104 > 88)

Example:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
Window positions and max values:
[1,3,-1] -> max = 3
[3,-1,-3] -> max = 3
[-1,-3,5] -> max = 5
[-3,5,3] -> max = 5
[5,3,6] -> max = 6
[3,6,7] -> max = 7
"""

from collections import deque
from typing import List

Check failure on line 19 in data_structures/arrays/sliding_window_maximum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/arrays/sliding_window_maximum.py:18:1: I001 Import block is un-sorted or un-formatted

class SlidingWindowMaximum:
def max_sliding_window(self, nums: List[int], k: int) -> List[int]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/sliding_window_maximum.py, please provide doctest for the function max_sliding_window

Please provide descriptive name for the parameter: k

if not nums:
return []

result = [] # Stores max of each window
window = deque() # Stores indices of elements in current window

for i, num in enumerate(nums):
# Remove indices of elements outside current window
while window and window[0] <= i - k:
window.popleft()

# Remove indices of elements smaller than current num
while window and nums[window[-1]] < num:
window.pop()

window.append(i)

# Add the max for this window to result
if i >= k - 1:
result.append(nums[window[0]])

return result

# Example dry run
if __name__ == "__main__":
nums = [1,3,-1,-3,5,3,6,7]
k = 3
solver = SlidingWindowMaximum()
print("Sliding Window Maximum:", solver.max_sliding_window(nums, k))