Skip to content

Commit 86e3a55

Browse files
committed
SlidignWindow
1 parent 6e6d134 commit 86e3a55

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

SlidingWindow_AvgOfArray.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def find_avg_ofarray1(K, arr):
1616
result = []
1717
window_start = 0
1818
window_Sum = 0.0
19-
for windowEnd in range(len(arr)):
20-
windowSum += arr[windowEnd] # add the next element
19+
for window_End in range(len(arr)):
20+
window_Sum += arr[window_End] # add the next element
2121
# slide the window, we don't need to alide if we have not hit the required window size of 'k'
22-
if windowEnd >= K - 1:
23-
result.append(windowSum / K) # Calculate the Average
24-
windowSum -= arr[windowStart] # remove element going out
25-
windowStart += 1 # slide the windw by 1
22+
if window_End >= K - 1:
23+
result.append(window_End / K) # Calculate the Average
24+
window_Sum -= arr[window_start] # remove element going out
25+
window_start += 1 # slide the windw by 1
2626
return result
2727

2828
# Time Space is O(n)

SlidingWindow_FruitsintoBasket.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
Given an array of characters where each character represents a fruit tree, you are given two baskets and your goal is to put maximum number of fruits in each basket.
3+
The only restriction is that each basket can have only one type of fruit.
4+
You can start with any tree, but once you have started you can’t skip a tree. You will pick one fruit from each tree until you cannot,
5+
i.e., you will stop when you have to pick from a third fruit type.
6+
Write a function to return the maximum number of fruits in both the baskets.
7+
Example 1:
8+
Input: Fruit=['A', 'B', 'C', 'A', 'C']
9+
Output: 3
10+
Explanation: We can put 2 'C' in one basket and one 'A' in the other from the subarray ['C', 'A', 'C']
11+
Example 2:
12+
Input: Fruit=['A', 'B', 'C', 'B', 'B', 'C']
13+
Output: 5
14+
Explanation: We can put 3 'B' in one basket and two 'C' in the other basket.
15+
This can be done if we start with the second letter: ['B', 'C', 'B', 'B', 'C']
16+
'''
17+
18+
def fruits_into_baskets(fruits):
19+
window_start = 0
20+
max_length = 0
21+
fruit_freq = {}
22+
23+
# try to extend the range[window_start, window_end]
24+
for window_end in range(len(fruits)):
25+
right_fruit = fruits[window_end]
26+
if right_fruit not in fruit_freq:
27+
fruit_freq[right_fruit] = 0
28+
fruit_freq[right_fruit] += 1
29+
30+
#shrink the sliding window, untill we are left with '2' fruits in the fruit freq dictionary
31+
while len(fruit_freq) > 2:
32+
left_fruit = fruits[window_start]
33+
fruit_freq[left_fruit] -= 1
34+
if fruit_freq[left_fruit] == 0:
35+
del fruit_freq[left_fruit]
36+
window_start += 1 # shrink the window
37+
max_length = max(max_length, window_end - window_start + 1)
38+
return max_length
39+
40+
def main():
41+
print("Max num of Fruits:" + str(fruits_into_baskets(['A','B','C','A','C'])))
42+
print("Max num of Fruits:" + str(fruits_into_baskets(['A','B','C','B','B','C'])))
43+
44+
main()
45+
46+
'''
47+
Time Complexity #
48+
The time complexity of the above algorithm will be O(N) where ‘N’ is the number of characters in the input array.
49+
The outer for loop runs for all characters and the inner while loop processes each character only once,
50+
therefore the time complexity of the algorithm will be O(N+N) which is asymptotically equivalent to O(N).
51+
52+
Space Complexity #
53+
The algorithm runs in constant space O(1) as there can be a maximum of three types of fruits stored in the frequency map.
54+
55+
Similar Problems #
56+
Problem 1: Longest Substring with at most 2 distinct characters
57+
58+
Given a string, find the length of the longest substring in it with at most two distinct characters.
59+
60+
Solution: This problem is exactly similar to our parent problem.
61+
'''

SlidingWindow_LongestSubstringwithKChar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def longest_substring_with_k_dist(str,k):
3434
max_length = max(max_length, window_end - window_start + 1)
3535
return max_length
3636

37+
def main():
38+
print("Length of the longest Substring:" + str(longest_substring_with_k_dist("araaci",2)))
39+
print("Length of the longest Substring:" + str(longest_substring_with_k_dist("araaci",1)))
40+
print("Length of the longest Substring:" + str(longest_substring_with_k_dist("cbbebi",3)))
41+
main()
3742
'''
3843
Time Complexity #
3944
The time complexity of the above algorithm will be O(N) where ‘N’ is the number of characters in the input string.

0 commit comments

Comments
 (0)