From 6e4c025361c13057eabd6e738832b258d412c32a Mon Sep 17 00:00:00 2001 From: khums Date: Mon, 26 Sep 2022 19:52:38 +0500 Subject: [PATCH 1/8] Update solution.py --- Q1/solution.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Q1/solution.py b/Q1/solution.py index 4429a19..51368ca 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1 +1,17 @@ ## Add code below with answer clearly stated +def fact(n): + if n==1: + return 1 + else: + return n*fact(n-1) +def sumDigits(n): + if(n==0): + return 0 + mod = n%10 + return mod + sumDigits((n-mod)/10) + +if __name__ == '__main__': + i = fact(100) + sum = sumDigits(i) + print(i) # 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 + print(sum) # 675 From 73f9fd99baf83d660a5e107c69be1f357934fc5a Mon Sep 17 00:00:00 2001 From: khums Date: Mon, 26 Sep 2022 19:53:15 +0500 Subject: [PATCH 2/8] Update solution.py --- Q2/solution.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..23cbfbf 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -1,3 +1,4 @@ +# Executed this solution on leetcode and it worked fine there.. # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): @@ -5,4 +6,16 @@ # self.next = next class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + fast = slow = head + for _ in range(n): + fast = fast.next + if not fast: + return head.next + + while fast.next: + fast = fast.next + slow = slow.next + + slow.next = slow.next.next + return head From de8e192d4c0057f3e6c08ab0b2318dbca376a628 Mon Sep 17 00:00:00 2001 From: khums Date: Mon, 26 Sep 2022 19:55:01 +0500 Subject: [PATCH 3/8] Update solution.py --- Q3/solution.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..e4ac188 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,104 @@ +from typing import List + + +# code taken from geeks for geeks website and tweaked : https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/ class Solution: + # A utility function to find median of two integers + def medianOfTwoElements(self, a, b): + return (a + b) / 2 + + # A utility function to find median of three integers + def medianOfThreeElements(self, a, b, c): + + return a + b + c - max(a, max(b, c)) - min(a, min(b, c)) + + # A utility function to find a median of four integers + def medianOfFourElements(self, a, b, c, d): + Max = max(a, max(b, max(c, d))) + Min = min(a, min(b, min(c, d))) + return (a + b + c + d - Max - Min) / 2 + + # Utility function to find median of single array + def medianOfSingleArray(self, arr, n): + if (n == 0): + return -1 + if (n % 2 == 0): + return (arr[n / 2] + arr[n / 2 - 1]) / 2 + return arr[n / 2] + + def findMedianUtil(self, A, N, B, M): + + # If smaller array is empty, return median from second array + if (N == 0): + return self.medianOfSingleArray(B, M) + + # If the smaller array has only one element + if (N == 1): + + # Case 1: If the larger array also has one element, + # simply call MO2() + if (M == 1): + return self.medianOfTwoElements(A[0], B[0]) + + # Case 2: If the larger array has odd number of elements, + # then consider the middle 3 elements of larger array and + # the only element of smaller array. Take few examples + # like following + # A = {9}, B[] = {5, 8, 10, 20, 30} and + # A[] = {1}, B[] = {5, 8, 10, 20, 30} + if (M & 1 != 0): + return self.medianOfTwoElements(B[M / 2], self.MO3(A[0], B[M / 2 - 1], B[M / 2 + 1])) + + # Case 3: If the larger array has even number of element, + # then median will be one of the following 3 elements + # ... The middle two elements of larger array + # ... The only element of smaller array + return self.medianOfThreeElements(B[M // 2], B[M // 2 - 1], A[0]) + + # If the smaller array has two elements + elif (N == 2): + + # Case 4: If the larger array also has two elements, + # simply call MO4() + if (M == 2): + return self.medianOfFourElements(A[0], A[1], B[0], B[1]) + + # Case 5: If the larger array has odd number of elements, + # then median will be one of the following 3 elements + # 1. Middle element of larger array + # 2. Max of first element of smaller array and element + # just before the middle in bigger array + # 3. Min of second element of smaller array and element + # just after the middle in bigger array + if (M & 1 != 0): + return self.medianOfThreeElements(B[M / 2], max(A[0], B[M / 2 - 1]), min(A[1], B[M / 2 + 1])) + + # Case 6: If the larger array has even number of elements, + # then median will be one of the following 4 elements + # 1) & 2) The middle two elements of larger array + # 3) Max of first element of smaller array and element + # just before the first middle element in bigger array + # 4. Min of second element of smaller array and element + # just after the second middle in bigger array + return self.medianOfFourElements(B[M / 2], B[M / 2 - 1], max(A[0], B[M / 2 - 2]), min(A[1], B[M / 2 + 1])) + + idxA = (N - 1) / 2 + idxB = (M - 1) / 2 + + ''' if A[idxA] <= B[idxB], then median must exist in + A[idxA....] and B[....idxB] ''' + if (A[idxA] <= B[idxB]): + return self.findMedianUtil(A + idxA, N / 2 + 1, B, M - idxA) + + ''' if A[idxA] > B[idxB], then median must exist in + A[...idxA] and B[idxB....] ''' + return self.findMedianUtil(A, N / 2 + 1, B + idxA, M - idxA) + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + N = len(nums1) + M = len(nums2) + if (N > M): + return self.findMedianUtil(nums2, M, nums1, N); + return self.findMedianUtil(nums1, N, nums2, M) + From e20f9a797c9f597e127f86967d500e372feaca4f Mon Sep 17 00:00:00 2001 From: khums Date: Tue, 27 Sep 2022 21:39:56 +0500 Subject: [PATCH 4/8] Update solution.py --- Q3/solution.py | 133 +++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 99 deletions(-) diff --git a/Q3/solution.py b/Q3/solution.py index e4ac188..6edb07d 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,104 +1,39 @@ from typing import List +# code taken from https://github.com/neetcode-gh/leetcode/blob/main/python/4-median-of-two-sorted-arrays.py -# code taken from geeks for geeks website and tweaked : https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/ class Solution: - # A utility function to find median of two integers - def medianOfTwoElements(self, a, b): - return (a + b) / 2 - - # A utility function to find median of three integers - def medianOfThreeElements(self, a, b, c): - - return a + b + c - max(a, max(b, c)) - min(a, min(b, c)) - - # A utility function to find a median of four integers - def medianOfFourElements(self, a, b, c, d): - Max = max(a, max(b, max(c, d))) - Min = min(a, min(b, min(c, d))) - return (a + b + c + d - Max - Min) / 2 - - # Utility function to find median of single array - def medianOfSingleArray(self, arr, n): - if (n == 0): - return -1 - if (n % 2 == 0): - return (arr[n / 2] + arr[n / 2 - 1]) / 2 - return arr[n / 2] - - def findMedianUtil(self, A, N, B, M): - - # If smaller array is empty, return median from second array - if (N == 0): - return self.medianOfSingleArray(B, M) - - # If the smaller array has only one element - if (N == 1): - - # Case 1: If the larger array also has one element, - # simply call MO2() - if (M == 1): - return self.medianOfTwoElements(A[0], B[0]) - - # Case 2: If the larger array has odd number of elements, - # then consider the middle 3 elements of larger array and - # the only element of smaller array. Take few examples - # like following - # A = {9}, B[] = {5, 8, 10, 20, 30} and - # A[] = {1}, B[] = {5, 8, 10, 20, 30} - if (M & 1 != 0): - return self.medianOfTwoElements(B[M / 2], self.MO3(A[0], B[M / 2 - 1], B[M / 2 + 1])) - - # Case 3: If the larger array has even number of element, - # then median will be one of the following 3 elements - # ... The middle two elements of larger array - # ... The only element of smaller array - return self.medianOfThreeElements(B[M // 2], B[M // 2 - 1], A[0]) - - # If the smaller array has two elements - elif (N == 2): - - # Case 4: If the larger array also has two elements, - # simply call MO4() - if (M == 2): - return self.medianOfFourElements(A[0], A[1], B[0], B[1]) - - # Case 5: If the larger array has odd number of elements, - # then median will be one of the following 3 elements - # 1. Middle element of larger array - # 2. Max of first element of smaller array and element - # just before the middle in bigger array - # 3. Min of second element of smaller array and element - # just after the middle in bigger array - if (M & 1 != 0): - return self.medianOfThreeElements(B[M / 2], max(A[0], B[M / 2 - 1]), min(A[1], B[M / 2 + 1])) - - # Case 6: If the larger array has even number of elements, - # then median will be one of the following 4 elements - # 1) & 2) The middle two elements of larger array - # 3) Max of first element of smaller array and element - # just before the first middle element in bigger array - # 4. Min of second element of smaller array and element - # just after the second middle in bigger array - return self.medianOfFourElements(B[M / 2], B[M / 2 - 1], max(A[0], B[M / 2 - 2]), min(A[1], B[M / 2 + 1])) - - idxA = (N - 1) / 2 - idxB = (M - 1) / 2 - - ''' if A[idxA] <= B[idxB], then median must exist in - A[idxA....] and B[....idxB] ''' - if (A[idxA] <= B[idxB]): - return self.findMedianUtil(A + idxA, N / 2 + 1, B, M - idxA) - - ''' if A[idxA] > B[idxB], then median must exist in - A[...idxA] and B[idxB....] ''' - return self.findMedianUtil(A, N / 2 + 1, B + idxA, M - idxA) - def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: - N = len(nums1) - M = len(nums2) - if (N > M): - return self.findMedianUtil(nums2, M, nums1, N); - return self.findMedianUtil(nums1, N, nums2, M) - - + A, B = nums1, nums2 + total = len(nums1) + len(nums2) + half = total // 2 + + if len(B) < len(A): + A, B = B, A + + l, r = 0, len(A) - 1 + while True: + i = (l + r) // 2 # A + j = half - i - 2 # B + + Aleft = A[i] if i >= 0 else float("-infinity") + Aright = A[i + 1] if (i + 1) < len(A) else float("infinity") + Bleft = B[j] if j >= 0 else float("-infinity") + Bright = B[j + 1] if (j + 1) < len(B) else float("infinity") + + # partition is correct + if Aleft <= Bright and Bleft <= Aright: + # odd + if total % 2: + return min(Aright, Bright) + # even + return (max(Aleft, Bleft) + min(Aright, Bright)) / 2 + elif Aleft > Bright: + r = i - 1 + else: + l = i + 1 + +ans = Solution() +arr1 = [-5, 3, 6, 12, 15] +arr2 = [-12, -10, -6, -3, 4, 10] +print("Median of the two arrays is {}".format(ans.findMedianSortedArrays(arr1, arr2))) From b4ef39da97dd5d5d869e01e9cd570d0fe896addf Mon Sep 17 00:00:00 2001 From: khums Date: Tue, 27 Sep 2022 22:00:41 +0500 Subject: [PATCH 5/8] Update solution.py --- Q3/solution.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Q3/solution.py b/Q3/solution.py index 6edb07d..4b1fded 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,6 +1,8 @@ from typing import List # code taken from https://github.com/neetcode-gh/leetcode/blob/main/python/4-median-of-two-sorted-arrays.py +# https://www.youtube.com/watch?v=q6IEA26hvXc video explaination +# My explaination: https://youtu.be/yqj1h_kw_KY class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: From f09aa566b873096a13f3b64b75d11c06847ed78b Mon Sep 17 00:00:00 2001 From: khums Date: Tue, 27 Sep 2022 22:12:08 +0500 Subject: [PATCH 6/8] Update solution.py --- Q3/solution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Q3/solution.py b/Q3/solution.py index 4b1fded..30cc306 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -2,7 +2,7 @@ # code taken from https://github.com/neetcode-gh/leetcode/blob/main/python/4-median-of-two-sorted-arrays.py # https://www.youtube.com/watch?v=q6IEA26hvXc video explaination -# My explaination: https://youtu.be/yqj1h_kw_KY +# My explaination: https://www.youtube.com/watch?v=yqj1h_kw_KY class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: From 505abb8a731aaac1d6f15cf546f3106b2551edfe Mon Sep 17 00:00:00 2001 From: khums Date: Tue, 27 Sep 2022 22:15:58 +0500 Subject: [PATCH 7/8] Update solution.py --- Q2/solution.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Q2/solution.py b/Q2/solution.py index 23cbfbf..ecd8fd3 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -1,3 +1,5 @@ +# Video https://www.youtube.com/watch?v=-HyzeXs3xog + # Executed this solution on leetcode and it worked fine there.. # Definition for singly-linked list. # class ListNode: From a165b90a3e89fbb2bbc3e96d34aaa2ec994f5e1a Mon Sep 17 00:00:00 2001 From: khums Date: Tue, 27 Sep 2022 22:16:50 +0500 Subject: [PATCH 8/8] Update solution.py --- Q1/solution.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Q1/solution.py b/Q1/solution.py index 51368ca..875c018 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1,3 +1,4 @@ +# Video explaination: https://www.youtube.com/watch?v=2r5YV5h_-1I ## Add code below with answer clearly stated def fact(n): if n==1: