From 8232ccc8232489e7c268b2af509956210e9fa7b5 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 14:51:10 -0500 Subject: [PATCH 1/8] brutforce approach --- merge_sorted_array.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 merge_sorted_array.py diff --git a/merge_sorted_array.py b/merge_sorted_array.py new file mode 100644 index 00000000..b8844535 --- /dev/null +++ b/merge_sorted_array.py @@ -0,0 +1,18 @@ +# https://leetcode.com/problems/merge-sorted-array/ + + + +# brutforce Tc- O((m+n)log(m+n)) Sc- O(1) +class Solution: + def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None: + j = 0 + for i in range(m ,m +n): + nums1[i] = nums2[j] + j += 1 + nums1.sort() + return nums1 + + +solution = Solution() +print(solution.merge([1,2,3,0,0,0],3,[2,5,6],3)) + \ No newline at end of file From 726e32322b9a59e5d55740e047859c462a29040f Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 15:28:51 -0500 Subject: [PATCH 2/8] optimised solution --- merge_sorted_array.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/merge_sorted_array.py b/merge_sorted_array.py index b8844535..a452c2bb 100644 --- a/merge_sorted_array.py +++ b/merge_sorted_array.py @@ -1,17 +1,38 @@ # https://leetcode.com/problems/merge-sorted-array/ +# Using two pointers started updenting the elements from second array into the first array. While doing that, compare the last element of the first array with last element of second array and update accordinlgy - -# brutforce Tc- O((m+n)log(m+n)) Sc- O(1) +# Time Complexity- O((m+n) Space Complexity- O(1) class Solution: def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None: - j = 0 - for i in range(m ,m +n): - nums1[i] = nums2[j] - j += 1 - nums1.sort() - return nums1 + # finding the last index of the merge array + last_index= m + n - 1 + # merge from last index + while m > 0 and n > 0: + # checking whether the last element of first array is greater than the last element of second array + if nums1[m - 1] > nums2[n - 1]: + # update the last index of the nums1 with greater element + nums1[last_index] = nums1[m - 1] + # decrement the index + m -= 1 + # when the last element of second array is greater than the last element of first array + else: + nums1[last_index] = nums2[n - 1] + # decrement the index + n -= 1 + # decrement the last index + last_index -= 1 + + # update the remaining elements from second array into the first array + # check if n is greater than 0 after decrementing + while n > 0: + # update with the elements + nums1[last_index] = nums2[n-1] + # decrement the pointer + n -= 1 + last_index -= last_index + return nums1 solution = Solution() print(solution.merge([1,2,3,0,0,0],3,[2,5,6],3)) From ba8c2b8dd22c72fc1a429714a3de3b29cb07b1b4 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 15:33:59 -0500 Subject: [PATCH 3/8] optimised solution --- merge_sorted_array.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/merge_sorted_array.py b/merge_sorted_array.py index a452c2bb..83bcb98e 100644 --- a/merge_sorted_array.py +++ b/merge_sorted_array.py @@ -30,9 +30,8 @@ def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None: # update with the elements nums1[last_index] = nums2[n-1] # decrement the pointer - n -= 1 - last_index -= last_index - return nums1 + n = n - 1 + last_index = last_index - 1 solution = Solution() print(solution.merge([1,2,3,0,0,0],3,[2,5,6],3)) From 4139005c4c57db26e8e2d630a7f04f9314c37e27 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 16:04:53 -0500 Subject: [PATCH 4/8] brutforce --- remove_duplicate.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 remove_duplicate.py diff --git a/remove_duplicate.py b/remove_duplicate.py new file mode 100644 index 00000000..6317cc8a --- /dev/null +++ b/remove_duplicate.py @@ -0,0 +1,25 @@ +# https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ + +# brutforce Tc- O(2n) Sc -O(n) +class Solution: + def removeDuplicates(self, nums: list[int]) -> int: + k = 2 + map = {} + result = [] + for i in nums: + if i in map: + map[i] += 1 + else: + map[i] = 1 + if map[i] <= k: + result.append(i) + # in this problem need not to define any extra space so arranging num list again + for i in range(len(result)): + nums[i] = result[i] + return len(result) + + +solution = Solution() +print(solution.removeDuplicates([1,1,1,2,2,3])) +# nums = [1,1,1,2,2,3] +# output = 5 \ No newline at end of file From 2d1c77682ab51d76f01f310ce862c87695a5c2f6 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 16:28:47 -0500 Subject: [PATCH 5/8] optimised solution --- remove_duplicate.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/remove_duplicate.py b/remove_duplicate.py index 6317cc8a..fc5c0a0a 100644 --- a/remove_duplicate.py +++ b/remove_duplicate.py @@ -4,20 +4,28 @@ class Solution: def removeDuplicates(self, nums: list[int]) -> int: k = 2 - map = {} - result = [] - for i in nums: - if i in map: - map[i] += 1 + old = 0 + new = 0 + count = 0 + while new < len(nums): + # check whether the previous number is same as the current + if new != 0 and nums[new] == nums[new - 1]: + # increase the count + count += 1 else: - map[i] = 1 - if map[i] <= k: - result.append(i) - # in this problem need not to define any extra space so arranging num list again - for i in range(len(result)): - nums[i] = result[i] - return len(result) - + # if the first occurance + count = 1 + # check the count is in given limit + if count <= k: + # update the list with old + nums[old] = nums[new] + # increment the old + old += 1 + # increment the new + new += 1 + # return the old + return old + solution = Solution() print(solution.removeDuplicates([1,1,1,2,2,3])) From b799ba66d9420991dcd0f8932716037af52ce792 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 16:59:51 -0500 Subject: [PATCH 6/8] brutforce --- search_a_2d_matrix.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 search_a_2d_matrix.py diff --git a/search_a_2d_matrix.py b/search_a_2d_matrix.py new file mode 100644 index 00000000..86a0f429 --- /dev/null +++ b/search_a_2d_matrix.py @@ -0,0 +1,29 @@ +# https://leetcode.com/problems/search-a-2d-matrix-ii/ + +# brutforce O(m*n) +class Solution: + def searchMatrix(self, matrix: list[list[int]], target: int) -> bool: + m = len(matrix) + n = len(matrix[0]) + # apply binary search on rows + # same can be done on columns using + # for i in range(n) + # low, high = 0, m - 1 + for i in range(m): + low, high = 0, n - 1 + while low <= high: + mid = low + (high - low) // 2 + if matrix[i][mid] == target: + return True + elif matrix[i][mid] < target: + low = mid + 1 + else: + high = mid - 1 + + return n + +solution = Solution() +print(solution.searchMatrix([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], 5)) + +# target = 5 +# output = true From 96b04c11983c7d0a7e6287dfc76e75d9a9832dc3 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 17:33:07 -0500 Subject: [PATCH 7/8] optimal solution --- search_a_2d_matrix.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/search_a_2d_matrix.py b/search_a_2d_matrix.py index 86a0f429..81abee84 100644 --- a/search_a_2d_matrix.py +++ b/search_a_2d_matrix.py @@ -1,29 +1,30 @@ # https://leetcode.com/problems/search-a-2d-matrix-ii/ -# brutforce O(m*n) +# To find the target element, start checking from the top left corner of the matrix. If the target is less than the current element decrease the column as the matrix is sorted. and if it's greater than the current element increase the row. +# Time Complexity- O(m + n) Space Complexity- O(1) class Solution: def searchMatrix(self, matrix: list[list[int]], target: int) -> bool: m = len(matrix) n = len(matrix[0]) - # apply binary search on rows - # same can be done on columns using - # for i in range(n) - # low, high = 0, m - 1 - for i in range(m): - low, high = 0, n - 1 - while low <= high: - mid = low + (high - low) // 2 - if matrix[i][mid] == target: - return True - elif matrix[i][mid] < target: - low = mid + 1 - else: - high = mid - 1 + # starting from the top-right corner + r, c = 0 , n - 1 + + while r < m and c >= 0: + # if the target is present at this coordinate + if matrix[r][c] == target: + return True + # if the target is less than the element + elif matrix[r][c] > target: + # decrement the column + c -= 1 + else: + # increment the row + r += 1 - return n + return False solution = Solution() -print(solution.searchMatrix([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], 5)) +print(solution.searchMatrix([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], 19)) # target = 5 # output = true From 5f82edcc9de5b726e01783ff80240c43c51f766e Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 29 Nov 2025 19:12:35 -0500 Subject: [PATCH 8/8] required changes --- remove_duplicate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remove_duplicate.py b/remove_duplicate.py index fc5c0a0a..527dc4ac 100644 --- a/remove_duplicate.py +++ b/remove_duplicate.py @@ -1,6 +1,6 @@ # https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ -# brutforce Tc- O(2n) Sc -O(n) +# Time Complexity- O(n) Space Complexity- O(1) class Solution: def removeDuplicates(self, nums: list[int]) -> int: k = 2