diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..165d0d8a --- /dev/null +++ b/Problem1.py @@ -0,0 +1,22 @@ +# Time Complexity --> O(n) where n is the size of array +# Space Complexity --> O(1) +# Approach --> Use slow and fast pointers where the fast pointer keeps check on number of duplicates. If the count is more than 2, we reset the count and irrespective of the conditions the fast pointer moves. +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if len(nums)<=1: + return len(nums) + slow = 1 + fast = 1 + count = 1 + while fast O(m+n) where m and n are the number of elements from both arrays +# Space Complexity --> O(1) +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + p1 = m-1 + p2 = n-1 + d = m+n-1 + while p1>=0 and p2>=0: + if nums1[p1]>=nums2[p2]: + nums1[d] = nums1[p1] + p1 = p1-1 + else: + nums1[d] = nums2[p2] + p2 = p2-1 + d = d-1 + while p2>=0: + nums1[p2] = nums2[p2] + p2 = p2-1 diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..49dbee2e --- /dev/null +++ b/Problem3.py @@ -0,0 +1,15 @@ +# Time Complexity --> O(m+n) where m, n are the dimensions of matrix +# Space Complexity --> O(1) +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + r = len(matrix)-1 + c = 0 + + while r>=0 and c