Skip to content

Commit eca9bb2

Browse files
added 3 DSA questions in python
1 parent 788d95b commit eca9bb2

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Question:
3+
Given an array fruits representing types of fruit, pick a contiguous subarray containing at most 2 different types of fruit.
4+
Return the maximum number of fruits you can collect.
5+
6+
Example:
7+
Input: fruits = [1,2,1,2,3]
8+
Output: 4
9+
Explanation:
10+
Pick subarray [1,2,1,2] -> contains 2 types and length 4
11+
"""
12+
13+
from typing import List
14+
from collections import defaultdict
15+
16+
class FruitIntoBaskets:
17+
def total_fruit(self, fruits: List[int]) -> int:
18+
count = defaultdict(int) # Stores count of each fruit type
19+
left = 0 # Left pointer of sliding window
20+
max_fruit = 0 # Tracks max number of fruits collected
21+
22+
for right, fruit in enumerate(fruits):
23+
count[fruit] += 1 # Include current fruit
24+
25+
# Shrink window if more than 2 types
26+
while len(count) > 2:
27+
count[fruits[left]] -= 1
28+
if count[fruits[left]] == 0:
29+
del count[fruits[left]]
30+
left += 1
31+
32+
max_fruit = max(max_fruit, right - left + 1)
33+
34+
return max_fruit
35+
36+
# Example dry run
37+
if __name__ == "__main__":
38+
fruits = [1,2,1,2,3]
39+
solver = FruitIntoBaskets()
40+
print("Maximum Fruits Collected:", solver.total_fruit(fruits))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Question:
3+
Given a binary array nums and an integer k, find the length of the longest subarray containing 1s after flipping at most k zeros.
4+
5+
Example:
6+
Input: nums = [1,0,1,1,0,1], k = 1
7+
Output: 4
8+
Explanation:
9+
Flip the first 0 at index 1 -> subarray [1,1,1,1] has length 4
10+
"""
11+
12+
from typing import List
13+
14+
class LongestOnesAfterReplacement:
15+
def longest_ones(self, nums: List[int], k: int) -> int:
16+
left = 0 # Left pointer of sliding window
17+
max_len = 0 # Tracks maximum window length
18+
zeros_count = 0 # Count of zeros in current window
19+
20+
for right in range(len(nums)):
21+
if nums[right] == 0:
22+
zeros_count += 1
23+
24+
# Shrink window if zeros exceed k
25+
while zeros_count > k:
26+
if nums[left] == 0:
27+
zeros_count -= 1
28+
left += 1
29+
30+
max_len = max(max_len, right - left + 1)
31+
32+
return max_len
33+
34+
# Example dry run
35+
if __name__ == "__main__":
36+
nums = [1,0,1,1,0,1]
37+
k = 1
38+
solver = LongestOnesAfterReplacement()
39+
print("Longest Ones After Replacement:", solver.longest_ones(nums, k))
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Question:
3+
Given an integer array nums and an integer k, return the maximum value in each sliding window of size k.
4+
5+
Example:
6+
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
7+
Output: [3,3,5,5,6,7]
8+
Explanation:
9+
Window positions and max values:
10+
[1,3,-1] -> max = 3
11+
[3,-1,-3] -> max = 3
12+
[-1,-3,5] -> max = 5
13+
[-3,5,3] -> max = 5
14+
[5,3,6] -> max = 6
15+
[3,6,7] -> max = 7
16+
"""
17+
18+
from collections import deque
19+
from typing import List
20+
21+
class SlidingWindowMaximum:
22+
def max_sliding_window(self, nums: List[int], k: int) -> List[int]:
23+
if not nums:
24+
return []
25+
26+
result = [] # Stores max of each window
27+
window = deque() # Stores indices of elements in current window
28+
29+
for i, num in enumerate(nums):
30+
# Remove indices of elements outside current window
31+
while window and window[0] <= i - k:
32+
window.popleft()
33+
34+
# Remove indices of elements smaller than current num
35+
while window and nums[window[-1]] < num:
36+
window.pop()
37+
38+
window.append(i)
39+
40+
# Add the max for this window to result
41+
if i >= k - 1:
42+
result.append(nums[window[0]])
43+
44+
return result
45+
46+
# Example dry run
47+
if __name__ == "__main__":
48+
nums = [1,3,-1,-3,5,3,6,7]
49+
k = 3
50+
solver = SlidingWindowMaximum()
51+
print("Sliding Window Maximum:", solver.max_sliding_window(nums, k))

0 commit comments

Comments
 (0)