From ca87a812890c8a34e79ba5bcdd3e19675d9c2072 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Wed, 21 Jan 2026 19:23:33 -0800 Subject: [PATCH] Add Python solutions for all three two-pointers-2 problems --- Problem1_RemoveDuplicatesFromSortedArrayII.py | 24 +++++++++++++ Problem2_MergeSortedArray.py | 34 +++++++++++++++++++ Problem3_Search2DMatrixII.py | 32 +++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 Problem1_RemoveDuplicatesFromSortedArrayII.py create mode 100644 Problem2_MergeSortedArray.py create mode 100644 Problem3_Search2DMatrixII.py diff --git a/Problem1_RemoveDuplicatesFromSortedArrayII.py b/Problem1_RemoveDuplicatesFromSortedArrayII.py new file mode 100644 index 00000000..31bfda70 --- /dev/null +++ b/Problem1_RemoveDuplicatesFromSortedArrayII.py @@ -0,0 +1,24 @@ +# Time Complexity : O(n) where n is the length of the array +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Use two pointers where slow pointer tracks position to place next valid element. +# Fast pointer traverses array, and we allow at most 2 occurrences by checking if current element equals element at slow-2. +# Only place element at slow pointer if it doesn't create more than 2 duplicates. + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if len(nums) <= 2: + return len(nums) + + slow = 2 # Start from index 2 since first two elements are always valid + + for fast in range(2, len(nums)): + # If current element is different from element at slow-2, it's safe to place + # This ensures at most 2 occurrences of any element + if nums[fast] != nums[slow - 2]: + nums[slow] = nums[fast] + slow += 1 + + return slow diff --git a/Problem2_MergeSortedArray.py b/Problem2_MergeSortedArray.py new file mode 100644 index 00000000..6dc37307 --- /dev/null +++ b/Problem2_MergeSortedArray.py @@ -0,0 +1,34 @@ +# Time Complexity : O(m + n) where m and n are lengths of nums1 and nums2 +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Use two pointers starting from the end of both arrays, comparing and placing larger elements at the end of nums1. +# Start from the last valid positions (m-1 for nums1, n-1 for nums2) and work backwards. +# This avoids overwriting unprocessed elements in nums1 since we're filling from the end. + +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. + """ + # Pointers for nums1, nums2, and the end of merged array + i = m - 1 + j = n - 1 + k = m + n - 1 + + # Merge from the end + while i >= 0 and j >= 0: + if nums1[i] > nums2[j]: + nums1[k] = nums1[i] + i -= 1 + else: + nums1[k] = nums2[j] + j -= 1 + k -= 1 + + # Copy remaining elements from nums2 if any + while j >= 0: + nums1[k] = nums2[j] + j -= 1 + k -= 1 diff --git a/Problem3_Search2DMatrixII.py b/Problem3_Search2DMatrixII.py new file mode 100644 index 00000000..2cb85ab8 --- /dev/null +++ b/Problem3_Search2DMatrixII.py @@ -0,0 +1,32 @@ +# Time Complexity : O(m + n) where m is rows and n is columns +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Start from top-right corner (or bottom-left) and use two pointers to traverse. +# If current element is greater than target, move left (eliminate column); if smaller, move down (eliminate row). +# This leverages the sorted property of both rows and columns to efficiently narrow down the search space. + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + if not matrix or not matrix[0]: + return False + + m, n = len(matrix), len(matrix[0]) + # Start from top-right corner + row = 0 + col = n - 1 + + while row < m and col >= 0: + current = matrix[row][col] + + if current == target: + return True + elif current > target: + # Current element is too large, eliminate this column + col -= 1 + else: + # Current element is too small, eliminate this row + row += 1 + + return False