From 3b00cdfdd1970d3aa41219e3d252573b4fa69a20 Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Wed, 4 Mar 2026 08:15:44 -0800 Subject: [PATCH 1/2] Two Pointers -2 done --- Problem2.py | 33 +++++++++++++++++++++++++++++++++ Problem3.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Problem2.py create mode 100644 Problem3.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..9cb01e27 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,33 @@ +#Merge Sorted Array +# Time complexity O(m+n) +# Space complexity -> O1 +# Logic -> Start from updating the biggest elemnt in the correct place this way when we overwrite the bigger items +# with the smaller items bigger items won't be lost as they will already be copied in the correct position + +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. + """ + smallListPointer = n-1 + bigListpointer = m-1 + finalListPOinter = len(nums1)-1 + + while smallListPointer >=0 and bigListpointer>=0: + nums1item = nums1[bigListpointer] + nums2Item = nums2[smallListPointer] + if nums1item > nums2Item: + nums1[finalListPOinter] = nums1item + bigListpointer-=1 + else: + nums1[finalListPOinter] = nums2Item + smallListPointer-=1 + finalListPOinter-=1 + + if bigListpointer == -1: + while finalListPOinter >= 0: + nums1[finalListPOinter] = nums2[smallListPointer] + finalListPOinter-=1 + smallListPointer-=1 + + \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..b3057b98 --- /dev/null +++ b/Problem3.py @@ -0,0 +1,30 @@ +#Search a 2D Matrix II + +# Time complexity O(m+n) where m is rows and n is columns +# Space complexity -> O1 +# Logic -> start from last row 1st column. based on the target comparison we can move up if target is lower than cuurrnt element if bigger move right +# keep on moving until you find element or you cross the matrix boundary which means target is not there + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + startRow = len(matrix)-1 + startColumn = 0 + maxColumns = len(matrix[0]) + + while startRow>=0 and startColumn currentItem: + startColumn+=1 + + return False + + \ No newline at end of file From 852579b279cf7ac128e20aec1950030ee4e89599 Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Wed, 4 Mar 2026 08:16:41 -0800 Subject: [PATCH 2/2] missed 1 file --- Problem1.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..9b1f3e48 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,27 @@ +#Remove Duplicates from Sorted Array II + +# Time comlexity -> O(n) +# Space complexity -> O1 +# Logic -> maitain 2 pointers, slow and fast, fast will take care of occurences, for 1st 2 ocurences it will update the slow with fast value +# once occurences move above 2 it's just on keeps moving ahead to find new number that needs to be moved in place of slow pointer's value + +from typing import List +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + slow = 0 + fast = 0 + length = len(nums) + count=0 + k = 2 + for fast in range(0,length): + if (fast != 0 and nums[fast]==nums[fast-1]): + count+=1 + else: + count = 1 + + if count <=k: + nums[slow]=nums[fast] + slow+=1 + + return slow +