From e615174afc0e7b3e9baa3b981b97a67d67a3f543 Mon Sep 17 00:00:00 2001 From: Sarvesh Sawant Date: Thu, 25 Dec 2025 15:49:08 -0500 Subject: [PATCH] Solve all problems --- Problem1.java | 21 +++++++++++++++++++++ Problem2.java | 30 ++++++++++++++++++++++++++++++ Problem3.java | 25 +++++++++++++++++++++++++ 3 files changed, 76 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..880e7380 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,21 @@ +/* +80. Remove Duplicates from Sorted Array II +TC: O(n) SC: O(1) +First 2 elements are already valid, put slow and fast on index 2, fast tracks index of new elements to put on index slow, at every point check if the new element on fast is valid by checking if the element has occured has less than 2 times by checking slow - k, this works since array is sorted, since if the element has occured less than 2 times slow - k will not be equal to element on index fast. +*/ +class Solution { + public int removeDuplicates(int[] nums) { + int k = 2; // Max 2 duplicates are allowed + int slow = k, fast = k; // First 2 elements in nums are always valid + + for(int i = k; i < nums.length; i++){ + if(nums[slow - k] != nums[fast]){ // Check if the element has not occured more than k times + nums[slow] = nums[fast]; // Store it + slow++; + } + fast++; // Move to next element + } + + return slow; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..32293ee9 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,30 @@ +/* +88. Merge Sorted Array +Ran on leetcode: Yes +TC: O(m + n) SC: O(1) +Start at last index of nums1, nums2 and result, since that would avoid loss of values of nums1. Check if element in nums1 is greater than nums2 and place in result and vice versa. Fill rest of the element remaining in nums2 if necessary. +*/ +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + + int p1 = m - 1, p2 = n - 1, r1 = m + n - 1; // Pointer on last index of nums1, nums2 and result array + + + while(p1 >= 0 && p2 >= 0){ // Fill elements from end of result array, to not loose elements from nums1 + if(nums1[p1] > nums2[p2]){ // If the element on nums1 is greater than nums2 place nums1 element + nums1[r1] = nums1[p1]; // Place on last unfilled index of result + p1--; + } else { // If the element on nums2 is greater than nums1 place nums1 element + nums1[r1] = nums2[p2]; // Place on last unfilled index of result + p2--; + } + r1--; + } + + while(p2 >= 0){ // Fill rest of the elements of nums2, since nums1 is result the element in nums1 are already at right place + nums1[r1] = nums2[p2]; + p2--; + r1--; + } + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..79bfc064 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,25 @@ +/* +240. Search a 2D Matrix II +Ran on leetcode: Yes +TC: O(m + n) SC: O(1) +Since the row and column are both sorted, we can start at top right corner, move down if the target is greater than current element and move left if the target is smaller until we find the target or return false. +*/ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + + int cLen = matrix[0].length, rLen = matrix.length; // Dimensions of matrix + int c = cLen - 1, r = 0; // Top - Right column index + + while(r < rLen && c >= 0){ // Index is valid + if(matrix[r][c] == target){ // Target found + return true; + } else if(matrix[r][c] > target){ // Element is greater than target + c--; // Move left + } else { // Element is smaller than target + r++; // Move down + } + } + + return false; // Element not found + } +} \ No newline at end of file