From 23549d1afe23c2891b5623c3e070b96d2b0159cf Mon Sep 17 00:00:00 2001 From: dhruvil15 <76967484+dhruvil15@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:40:43 -0400 Subject: [PATCH] Complete Two-Pointer-2 --- Problem37.java | 30 ++++++++++++++++++++++++++++++ Problem38.java | 23 +++++++++++++++++++++++ Problem39.java | 32 ++++++++++++++++++++++++++++++++ Sample.java | 7 ------- 4 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 Problem37.java create mode 100644 Problem38.java create mode 100644 Problem39.java delete mode 100644 Sample.java diff --git a/Problem37.java b/Problem37.java new file mode 100644 index 00000000..342bb8b1 --- /dev/null +++ b/Problem37.java @@ -0,0 +1,30 @@ +// Time Complexity : O(m+n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int p1 = m-1; int p2 = n-1; + int idx = m+n-1; + + while(p1 >= 0 && p2 >= 0) { + if (nums2[p2] > nums1[p1]) { + nums1[idx] = nums2[p2]; + p2--; + } else { + nums1[idx] = nums1[p1]; + p1--; + } + idx--; + } + + while(p2 >= 0) { + nums1[idx] = nums2[p2]; + p2--; + idx--; + } + } +} \ No newline at end of file diff --git a/Problem38.java b/Problem38.java new file mode 100644 index 00000000..f38bb5fe --- /dev/null +++ b/Problem38.java @@ -0,0 +1,23 @@ +// Time Complexity : O(m+n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + + int r = 0; int c = n-1; + + while (r < m && c >= 0) { + if (matrix[r][c] == target) return true; + else if (matrix[r][c] > target) c--; + else r++; + } + + return false; + } +} \ No newline at end of file diff --git a/Problem39.java b/Problem39.java new file mode 100644 index 00000000..bf6215dd --- /dev/null +++ b/Problem39.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +class Solution { + public int removeDuplicates(int[] nums) { + int k = 2; + int slow = 0; int fast = 0; + + int count = 0; + + while (fast < nums.length) { + if (fast != 0 && nums[fast] == nums[fast-1]) { + count++; + } else { + count = 1; + } + + if (count <= k) { + nums[slow] = nums[fast]; + slow++; + } + + fast++; + } + + return slow; + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 960e272d..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ - -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Three line explanation of solution in plain english - -// Your code here along with comments explaining your approach