-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
added 3 DSA questions in python #13373
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
Changes from 1 commit
eca9bb2
6da352f
3d2a1a1
a43dcae
5755807
f53b67d
7047193
e245324
ccc32fd
6d7bb8f
1b2ccf2
1515c02
b7ce1d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| 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 | ||
| from collections import defaultdict | ||
|
|
||
| class FruitIntoBaskets: | ||
| def total_fruit(self, fruits: List[int]) -> int: | ||
| 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)) | ||
| 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. | ||
|
|
||
| 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
|
||
|
|
||
| class LongestOnesAfterReplacement: | ||
| def longest_ones(self, nums: List[int], k: int) -> int: | ||
|
||
| 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)) | ||
| 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. | ||
|
|
||
| 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 | ||
|
|
||
| class SlidingWindowMaximum: | ||
| def max_sliding_window(self, nums: List[int], k: int) -> List[int]: | ||
|
||
| 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)) | ||
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.
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 functiontotal_fruit