Skip to content

Commit 6e6d134

Browse files
committed
SlidingWidnowPractice
1 parent d7e7596 commit 6e6d134

6 files changed

+184
-3
lines changed

LinkedList_AddTwonumbers.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class ListNode:
2+
def _init_(self,value,next= None):
3+
self.value = value
4+
self.next = next
5+
#Approach 1
6+
class Solution2:
7+
def addTwoNumbers2(self, l1: ListNode, l2: ListNode,c = 0) -> ListNode:
8+
9+
val = l1.val + l2.val + c
10+
c = val // 10
11+
ret = ListNode(val % 10 )
12+
13+
if (l1.next != None or l2.next != None or c != 0):
14+
if l1.next == None:
15+
l1.next = ListNode(0)
16+
if l2.next == None:
17+
l2.next = ListNode(0)
18+
ret.next = self.addTwoNumbers2(l1.next,l2.next,c)
19+
return ret
20+
21+
#Approach 2
22+
class Solution:
23+
def addTwoNumber(self, l1: ListNode, l2: ListNode) -> ListNode :
24+
result = ListNode(0)
25+
l3 = result
26+
carry = 0
27+
28+
while l1 or l2 or carry:
29+
currentSum = 0
30+
currentSum += carry
31+
32+
if l1:
33+
currentSum += l1.value
34+
if l2:
35+
currentSum += l2.value
36+
37+
digit = currentSum % 10
38+
carry = currentSum // 10
39+
40+
l3.val = digit
41+
if l1 or l2 or carry:
42+
l3.next = ListNode(0)
43+
l3 = l3.next
44+
return result
45+

LinnkdList_IntersectionOfTwoLL.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
There are multiple approach to check the inersection of Two linked list
3+
A) The Brute force
4+
Iterate trhough LL A with length M and Iterate through LL B with length N.
5+
The overall time complexity is O(M*N)
6+
(B) ## APPROACH : 2 POINTER ##
7+
8+
## LOGIC ##
9+
# 1) Find the length of both lists;
10+
# 2) In the biggest list, advance its head N times where N is the length difference between the two lists.
11+
# 3) Now both lists have the same length, just iterate them and check for node equality.
12+
13+
## TIME COMPLEXITY : O(M+N) ##
14+
## SPACE COMPLEXITY : O(1) ##
15+
'''
16+
class ListNode:
17+
def _init_(self,value, next = None):
18+
self.next = next
19+
self.value = value
20+
class Solution:
21+
def getIntersection(self, headA:ListNode, headB:ListNode)-> ListNode:
22+
la = lb = 0
23+
m, n = headA, headB
24+
25+
#Find the length of both lists
26+
while(headA):
27+
la += 1
28+
headA = headA.next
29+
while(headB):
30+
lb += 1
31+
headB = headB.next
32+
33+
# 2) In the biggest list, advance its head N times where N is the length difference between the two lists.
34+
if(la > lb):
35+
diff = la - lb
36+
while(diff):
37+
m = m.next
38+
diff -= 1
39+
else:
40+
diff = lb - la
41+
while(diff):
42+
n= n.next
43+
diff -= 1
44+
45+
# 3) Now both lists have the same length, just iterate them and check for node equality.
46+
while( m and n):
47+
if(m == n): return m
48+
m = m.next
49+
n = n.next
50+
return None

SlidingWindow_Fix_Avg_Sum.py renamed to SlidingWindow_AvgOfArray.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ def find_avg_ofarray(K, arr):
1212
# Time space is O(n * k)
1313

1414
##### Optimised Approach ########
15-
def find_avg_ofarray(K, arr):
15+
def find_avg_ofarray1(K, arr):
1616
result = []
17-
windowStart = 0, windowSum = 0.0
17+
window_start = 0
18+
window_Sum = 0.0
1819
for windowEnd in range(len(arr)):
1920
windowSum += arr[windowEnd] # add the next element
2021
# slide the window, we don't need to alide if we have not hit the required window size of 'k'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Given a string, find the length of the longest substring in it with no more than K distinct characters
3+
Input: String="araaci", K=2
4+
Output: 4
5+
Explanation: The longest substring with no more than '2' distinct characters is "araa".
6+
Input: String="araaci", K=1
7+
Output: 2
8+
Explanation: The longest substring with no more than '1' distinct characters is "aa".
9+
Solution:
10+
->fisrt we will add char in charfreq HashMap
11+
->then We will expand window by adding element at window_end
12+
->once we reach greater then k then shrink the window by removing element from start
13+
'''
14+
def longest_substring_with_k_dist(str,k):
15+
window_start = 0
16+
max_length = 0
17+
char_freq = {}
18+
19+
# in the following loop we'll try to expand[window_start,window_end]
20+
for window_end in range(len(str)):
21+
right_char = str[window_end]
22+
if right_char not in char_freq:
23+
char_freq[right_char] = 0
24+
char_freq[right_char] += 1
25+
26+
# Shrink the sliding window, untill we are left with 'k' disctinct char in the char_freq
27+
while len(char_freq) > k:
28+
left_char = str[window_start]
29+
char_freq[left_char] -= 1
30+
if char_freq[left_char] == 0:
31+
del char_freq[left_char]
32+
window_start += 1 # shrink the window
33+
# remember the maximum length so far
34+
max_length = max(max_length, window_end - window_start + 1)
35+
return max_length
36+
37+
'''
38+
Time Complexity #
39+
The time complexity of the above algorithm will be O(N) where ‘N’ is the number of characters in the input string.
40+
The outer for loop runs for all characters and the inner while loop processes each character only once,
41+
therefore the time complexity of the algorithm will be O(N+N) which is asymptotically equivalent to O(N).
42+
43+
Space Complexity #
44+
The space complexity of the algorithm is O(K)O(K), as we will be storing a maximum of ‘K+1’ characters in the HashMap.
45+
'''

SlidingWindow_MaximumOfSubArray.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Input: [2, 1, 5, 1, 3, 2], k=3
3+
Output: 9
4+
Explanation: Subarray with maximum sum is [5, 1, 3].
5+
#Approach1:
6+
Brute Force approach is two loop outer loop will go for the each number and inner loop will do the sum for i+k till we find the sum.
7+
this will take O(n*k) time
8+
9+
#Approach 2: Fix sliding Window
10+
Move the sliding window based on our requiremnt
11+
1) Subtract the element going out of the sliding window i.e., subtract the first element of the window.
12+
2) Add the new element getting included in the sliding window i.e., the element coming right after the end of the window.
13+
14+
O(N)
15+
'''
16+
def max_sub_aaray_of_size_k(arr,k):
17+
max_sum, window_sum = 0,0
18+
window_start = 0
19+
20+
21+
for window_end in range(len(arr)):
22+
window_sum += arr[window_end] # add the next element
23+
#slide the window, we don't need to slide if we have not hit the require window size of 'k'
24+
if window_end >= k-1:
25+
max_sum = max(max_sum,window_sum)
26+
window_sum -= arr[window_start] # subtract the elemnt going out
27+
window_start += 1 # move the window towards the next
28+
return max_sum
29+
30+
def main():
31+
print("Max if sub array of size k"+ str(max_sub_aaray_of_size_k(2,[2,3,4,1,5])))
32+
33+
main()

SlidingWindow_Variable_SmallestSubArray.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
'''
2+
Problem: Given an array of positive numbers and a positive number ‘S’,
3+
find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. Return 0, if no such subarray exists.
4+
Input: [2, 1, 5, 2, 3, 2], S=7
5+
Output: 2
6+
Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2].
7+
'''
18
import math
29
def smallest_Subarray_with_Sum(arr,S):
310
# Taking Start of sliding window and initial window sum with min_length
411
window_sum = 0
512
window_start = 0
613
min_length = math.inf
714

8-
for window_end in range(0,len(arr)): # taking window end and creating slidin window
15+
for window_end in range(len(arr)): # taking window end and creating slidin window
916
window_sum += arr[window_end] # adding eliment of sliding window
1017

1118
while window_sum >= S: #Checking for Sum

0 commit comments

Comments
 (0)