From 64171b4767525aebfe3b8a20cafa6a9a0d612c8d Mon Sep 17 00:00:00 2001 From: takudzwa Date: Sun, 22 Feb 2026 20:42:12 -0500 Subject: [PATCH 1/2] [removeDuplicates, merge 2 sorted arrays, searchMatrix] --- Sample.java | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Sample.java b/Sample.java index 960e272d..657c7b8d 100644 --- a/Sample.java +++ b/Sample.java @@ -5,3 +5,71 @@ // Three line explanation of solution in plain english // Your code here along with comments explaining your approach + +public class Sample { + + // Time Complexity : O(n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : yes + public int removeDuplicates(int[] nums) { + int i = 0; + int k = 0; + int count = 1; + int prev = 10001; + while ( i < nums.length) { + if (prev != nums[i]) { + count = 1; + } + if (count <= 2) { + nums[k] = nums[i]; + k++; + count++; + } + prev = nums[i]; + i++; + } + return k; + } + + // Time Complexity : O(m + n) + // Space Complexity : O(1) no extra space + // Did this code successfully run on Leetcode : yes + + // start inserting from m - 1 index + // if m becomes 0 first; then just add the remaining elements from nums2 to nums1 + public void merge(int[] nums1, int m, int[] nums2, int n) { + int l = nums1.length - 1; + m--; + n--; + while (m >= 0 && n >= 0) { + nums1[l--] = nums1[m] < nums2[n] ? nums2[n--] : nums1[m--]; + } + while (n >= 0) { + nums1[l--] = nums2[n--]; + } + } + + // Time Complexity : O(m + n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : yes + + // start searching from bottom left corner of matrix + // if curr element is greater than target search upwards + // else search rightwards + // return true if found + public boolean searchMatrix(int[][] matrix, int target) { + int i = matrix.length - 1; + int j = 0; + while (i >=0 && j < matrix[0].length) { + if (matrix[i][j] == target) { + return true; + } + if (matrix[i][j] > target) { + i--; + } else { + j++; + } + } + return false; + } +} \ No newline at end of file From a783134974313ea136fccd776acf7f6aa82eeae3 Mon Sep 17 00:00:00 2001 From: takudzwa Date: Sun, 22 Feb 2026 20:45:13 -0500 Subject: [PATCH 2/2] [removeDuplicates, merge 2 sorted arrays, searchMatrix] --- Sample.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sample.java b/Sample.java index 657c7b8d..7ea02e2a 100644 --- a/Sample.java +++ b/Sample.java @@ -11,6 +11,9 @@ public class Sample { // Time Complexity : O(n) // Space Complexity : O(1) // Did this code successfully run on Leetcode : yes + // set prev to 10001 and count 1 initially + // if count is less than or equal 2 the put the element in at index k + // return k public int removeDuplicates(int[] nums) { int i = 0; int k = 0;