From 531acc10b05ed511baf98c8eaa81918c995fbcb9 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 19 Sep 2022 07:14:37 +0000 Subject: [PATCH 1/5] Update solution.py Answer of First question --- Q1/solution.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Q1/solution.py b/Q1/solution.py index 4429a19..7057675 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1 +1,24 @@ ## Add code below with answer clearly stated +def factorial(num): + if num==1: + return 1 + else: + var = 1 + for i in range(1, num): + var *= i + return var +## The above function will be used to calculate the factorial of given number. +def sum_factorial(num): + if num < 1: #if given number is negative or zero its a invalid number + print("Invalid Value!!!") + return None + if num == 1: + return 1 + else: + fact_num = factorial(n) # calculate the factorial of tgiven number + var = 0 # initiallize a veriable + for i in str(fact_num): #First convert the number to string to iteratively choose one digit from the output + var += int(i) # add that number to the Var that is initially zero + return var + +print("The sum of factorial is ", sum_factorial(100)) From f002f053aac09e6dfe42f76ee5eaecd226ecb7a3 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 19 Sep 2022 07:26:28 +0000 Subject: [PATCH 2/5] Update solution.py Added the solution for the second question --- Q2/solution.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..db4fc0e 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -5,4 +5,24 @@ # self.next = next class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + temp = head + c = 0 + while temp: #Find the length of the linked list + c+=1 + temp = temp.next + a = c-n+1 #Calculate the node to be removed + i = 1 + temp2 = head + prev = None + while i < a: #Traverse till the node to be removed + i+=1 + prev = temp2 #Prev pointer to point the previous node of the deletion node + temp2 = temp2.next + if temp2==head: + return head.next + prev.next = temp2.next #Link the previous node to the next of the deletion node + return head + + # I've done this problem already 0n 02/21/2022 17:58 at that time my submission was faster than 97% people at leetcode. + # Myprofile link : https://leetcode.com/umikhaan/ From 2781e0a175956d930de16da00ff58ee88a6160cf Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 19 Sep 2022 07:39:10 +0000 Subject: [PATCH 3/5] Update solution.py Solution of Question 4. --- Q3/solution.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..3edd48e 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,49 @@ class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + if len(nums1+nums2)%2!=0: # check the length of the solution + return sorted((nums1+nums2))[len(nums1+nums2)//2]/1 # Simple code to return the median if the length is odd + else: + return sum(sorted((nums1+nums2))[(len(nums1+nums2)//2)-1:(len(nums1+nums2)//2)+1])/2 # Simple code to return the median if the length is even. + #It is not efficient code. but easy to understand and simplest approach to my knowledge. + + +# in this case runtime increases but its memory efficient. +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + a = [] + half = (len(nums1) + len(nums2)) + idx = 0 + idy = 0 + + while True: + + if idx == len(nums1): + a += nums2[idy:] + break + elif idy == len(nums2): + a += nums1[idx:] + break + if nums1[idx] < nums2[idy]: + a.append(nums1[idx]) + idx += 1 + else: + a.append(nums2[idy]) + idy += 1 + return a[half // 2] if half % 2 != 0 else (a[half //2] + a[(half - 1) // 2]) / 2 + # While this one is more efficient,. +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + a = [] + half = (len(nums1) + len(nums2)) + + + while nums1 and nums2: + if nums1[0] < nums2[0]: + a.append(nums1.pop(0)) + else: + a.append(nums2.pop(0)) + + a += nums1 + nums2 + + return a[half // 2] if half % 2 != 0 else (a[half //2] + a[(half - 1) // 2]) / 2 From 1a21c8978674b317974282c0fcf4efb5def70a75 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 19 Sep 2022 07:56:44 +0000 Subject: [PATCH 4/5] Update solution.py --- Q1/solution.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Q1/solution.py b/Q1/solution.py index 7057675..9973cf8 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1,6 +1,9 @@ ## Add code below with answer clearly stated def factorial(num): - if num==1: + if num < 0: #if given number is negative or zero its a invalid number + print("Not Defined!!!") + return None + if num==1 or num== 0: return 1 else: var = 1 From 3c1123e2a5214c78fb77abd734817a288abf025c Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 19 Sep 2022 07:56:59 +0000 Subject: [PATCH 5/5] Update solution.py --- Q1/solution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Q1/solution.py b/Q1/solution.py index 9973cf8..795d95e 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -3,7 +3,7 @@ def factorial(num): if num < 0: #if given number is negative or zero its a invalid number print("Not Defined!!!") return None - if num==1 or num== 0: + elif num==1 or num== 0: return 1 else: var = 1