From 6b21f8abe79c1a10a15e7d3d95c469ee020dfba8 Mon Sep 17 00:00:00 2001 From: sainathek Date: Mon, 12 Jan 2026 02:44:12 -0500 Subject: [PATCH] Done two-pointers-2 --- problem1.java | 28 ++++++++++++++++++++++++++++ problem2.java | 30 ++++++++++++++++++++++++++++++ problem3.java | 30 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java create mode 100644 problem3.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..f22c7c35 --- /dev/null +++ b/problem1.java @@ -0,0 +1,28 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just needed to allow only 2 duplicates and shift in-place. + + +// Your code here along with comments explaining your approach in three sentences only +// Since array is sorted, same numbers come together, so we can control how many times we keep them. +// I use a write pointer and allow a number only if it is different from nums[write-2] (means max 2 allowed). +// This updates the array in-place and returns the new length. + +class Solution { + public int removeDuplicates(int[] nums) { + + if (nums.length <= 2) return nums.length; + + int write = 2; + + for (int i = 2; i < nums.length; i++) { + if (nums[i] != nums[write - 2]) { + nums[write] = nums[i]; + write++; + } + } + + return write; + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..41645c96 --- /dev/null +++ b/problem2.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, just needed to merge from end to avoid overwriting nums1. + + +// Your code here along with comments explaining your approach in three sentences only +// Since nums1 has extra space at the end, I fill it from the back so existing values don't get overwritten. +// I compare nums1[m-1] and nums2[n-1] and place the bigger one at the end pointer. +// Continue until nums2 is fully placed, because remaining nums1 part is already in correct position. + +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + + int i = m - 1; + int j = n - 1; + int k = m + n - 1; + + while (j >= 0) { + if (i >= 0 && nums1[i] > nums2[j]) { + nums1[k] = nums1[i]; + i--; + } else { + nums1[k] = nums2[j]; + j--; + } + k--; + } + } +} diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..5c3dc21c --- /dev/null +++ b/problem3.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, just needed to start from correct corner. + + +// Your code here along with comments explaining your approach in three sentences only +// I start from the top-right corner because from there I can eliminate one full row or column each step. +// If the current value is bigger than target, I move left, and if smaller, I move down. +// This way I never revisit cells and finish in O(m+n). + +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; + } +}