From 00df512ca7954677538df3c6a0135e7f6255a701 Mon Sep 17 00:00:00 2001 From: agk-s30 Date: Thu, 13 Aug 2026 19:52:50 -0700 Subject: [PATCH 1/3] Add merge function for merging sorted arrays Implement merge function to combine two sorted arrays in-place. --- Problem_2.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Problem_2.py diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..378b0e98 --- /dev/null +++ b/Problem_2.py @@ -0,0 +1,23 @@ +# https://leetcode.com/problems/merge-sorted-array/description/ + +# TC: O(n + m) +# SP: O(1) +# Explanation: Place two points at the end of each nums array, and another pointer at the nums1; +# keep comparing nums1 vs nums2 and insert the higher value, and in the end you will get the fully sorted array in nums1 + +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 + for p in range(n + m - 1, -1, -1): + if p2 < 0: + break + if p1 >= 0 and nums1[p1] > nums2[p2]: + nums1[p] = nums1[p1] + p1 -= 1 + else: + nums1[p] = nums2[p2] + p2 -= 1 From f8ed5d4b8c8deeca15f07266fe3a549c7eb8ab48 Mon Sep 17 00:00:00 2001 From: agk-s30 Date: Tue, 18 Aug 2026 21:33:25 -0700 Subject: [PATCH 2/3] Add solution for removing duplicates from sorted array Implement a solution to remove duplicates from a sorted array, allowing at most two occurrences of each element. --- Problem_1.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Problem_1.py diff --git a/Problem_1.py b/Problem_1.py new file mode 100644 index 00000000..b606624b --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,29 @@ +# https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/ + +# Time complexity: O(n) +# Space complexity: O(1) +# Explanation: Maintain two points, one to check count and one to act as the current index being written to. Keep iterating the elements and then return the 2nd pointer value. + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if not nums: + return 0 + + i = 1 + j = 1 + count = 1 + + while i < len(nums): + if nums[i] == nums[i-1]: + count += 1 + if count > 2: + i += 1 + continue + else: + count = 1 + nums[j] = nums[i] + j += 1 + i += 1 + + del nums[j:] + return j + 1 From 9de7da18b69a30df9a042c34ea4cbc1e6aec2d3a Mon Sep 17 00:00:00 2001 From: agk-s30 Date: Tue, 18 Aug 2026 21:35:04 -0700 Subject: [PATCH 3/3] Add searchMatrix function for 2D matrix search Implement searchMatrix method to find target in 2D matrix. --- Problem_3.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Problem_3.py diff --git a/Problem_3.py b/Problem_3.py new file mode 100644 index 00000000..2573d6cd --- /dev/null +++ b/Problem_3.py @@ -0,0 +1,20 @@ +# https://leetcode.com/problems/search-a-2d-matrix-ii/description/ + +# Time complexity: O(n) +# Space complexity: O(1) +# Explanation: Start from the bottom left. If the curr value is greater than target, shift up a row. If smaller, then shift right. Finally return target. + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + m, n = len(matrix), len(matrix[0]) + i, j = m - 1, 0 + + while i >=0 and j < n: + if matrix[i][j] > target: + i -= 1 + elif matrix[i][j] < target: + j += 1 + else: + return True + + return False