From 8f9a0dea168efdce66b664a6022290ab6cadac9f Mon Sep 17 00:00:00 2001 From: Manasvi Reddy Date: Fri, 19 Jun 2026 02:41:35 -0400 Subject: [PATCH] Done Two-Pointers-2 --- Problem1.py | 30 ++++++++++++++++++++++++++++++ Problem2.py | 28 ++++++++++++++++++++++++++++ Problem3.py | 24 ++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py create mode 100644 Problem3.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..df39987a --- /dev/null +++ b/Problem1.py @@ -0,0 +1,30 @@ +# Problem1 (https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) +# Time Complexity: O(n), fast pointer makes a single pass through the array +# Space Complexity: O(1), only a few extra variables used, no new array created + +# We use two pointers: fast to scan through every element, slow to mark where the next valid element goes. +# We count how many times each value repeats consecutively, since the array is sorted. +# If a value has appeared at most twice so far, we keep it by writing it to the slow position. + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + k = 2 # at most we can keep 2 occurrences of any number + slow = 0 # next position to write a valid (kept) element + fast = 0 # current position being scanned + count = 0 # keeps track of how many times the current value has appeared so far + + for i in range(len(nums)): + if fast != 0 and nums[fast] == nums[fast-1]: # we're checking if there's a previous INDEX to compare, + count += 1 # same value as previous, increase count + else: + count = 1 # different value (or first element), reset count to 1 + + if count <= k: # this occurrence is still within the allowed limit + nums[slow] = nums[fast] # write it to the next valid slot + slow += 1 # move slow forward since we just filled a valid position + + fast += 1 # always move fast forward to check the next element + + return slow # slow = total count of valid elements kept = k (the answer length) + + \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..0edfa363 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,28 @@ +# Problem2 (https://leetcode.com/problems/merge-sorted-array/) +# Time Complexity: O(m + n),every element from both arrays is visited and placed exactly once +# Space Complexity: O(1), merging is done in-place using nums1's existing slots, no new array created + +# We fill nums1 from the back, comparing the last unplaced elements of nums1 and nums2. +# We place the bigger of the two at the current end position and move that pointer backward. +# Once nums1's real elements are exhausted, any leftover elements in nums2 are copied directly. + +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, p2, p3 = m-1, n-1, m+n-1 # p1: last real elem in nums1, p2: last elem in nums2, p3: last slot to fill + + while p1 >= 0 and p2 >= 0: # keep going while both arrays still have unplaced elements + if nums2[p2] > nums1[p1]: # nums2's value is bigger + nums1[p3] = nums2[p2] # place it at the current end slot + p2 -= 1 # move nums2's pointer back + else: # nums1's value is bigger or equal + nums1[p3] = nums1[p1] # place it at the current end slot + p1 -= 1 # move nums1's pointer back + p3 -= 1 # move to the next slot (one step left) + + while p2 >= 0: # if nums2 still has leftover elements (nums1 ran out first) + nums1[p3] = nums2[p2] # copy them directly,they're already smaller than everything placed + p2 -= 1 + p3 -= 1 \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..72f3dadb --- /dev/null +++ b/Problem3.py @@ -0,0 +1,24 @@ +# Problem3 (https://leetcode.com/problems/search-a-2d-matrix-ii/) +# Time Complexity: O(m + n), row only increases up to m times, column only decreases up to n times +# Space Complexity: O(1), only a few extra variables used, no extra data structures + +# We start from the top-right corner, where left means smaller and down means bigger. +# We compare the current value to the target and move left if too big, or down if too small. +# We keep narrowing the search until we find the target or move out of the matrix bounds. + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + m = len(matrix) # number of rows + n = len(matrix[0]) # number of columns + + row, column = 0, n-1 # start at top-right corner: row 0, last column + + while row < m and column >= 0: # keep searching while still inside the matrix bounds + if matrix[row][column] == target: # found the target + return True + elif matrix[row][column] > target: # current value too big, target can't be right or below + column -= 1 # move left to find a smaller value + else: # current value too small, target can't be left or above + row += 1 # move down to find a bigger value + + return False \ No newline at end of file