diff --git a/DuplicatesInSortedArray.java b/DuplicatesInSortedArray.java new file mode 100644 index 00000000..294bd6b2 --- /dev/null +++ b/DuplicatesInSortedArray.java @@ -0,0 +1,38 @@ +// Time Complexity : O(N), touching each element twice O(2N) = ~O(N) +// Space Complexity : O(1), Updating the array in place +// Did this code successfully run on Leetcode : Yes +// Approach : + +// We need to identify the count of each value in the array and keep min(count, k) values of that elements. +// We can keep a count variable to count the frequency, which should be reset before calculating the frequency of the next elements. +// In order to track which elements needs to be replaced by which value, we can have two pointers starting from same position, +// but moving at fast and slow pace. +// The slow pointer indicates the index that needs to be updated and fast pointer indicates the value, the index +// needs to be updated with. +public class DuplicatesInSortedArray { + public int removeDuplicates(int[] nums) { + int len = nums.length; + int slow = 0; + int fast = 0; + int k = 2; + while(fast < len){ + int count = 0; + // Keep the value, needed for frequency calculation and replacing the slow pointer value later + int currValue = nums[fast]; + while(fast < len && nums[fast] == currValue) { + fast++; + count++; + } + // Repeat value only min( current value count, given value k) times + int minHops = Math.min(count, k); + while(minHops != 0){ + //Override the duplicate values with required values of the next element. + nums[slow] = currValue; + slow++; + minHops--; + } + } + + return slow; + } +} diff --git a/MergeSortedArray.java b/MergeSortedArray.java new file mode 100644 index 00000000..b7c547e9 --- /dev/null +++ b/MergeSortedArray.java @@ -0,0 +1,35 @@ +// Time Complexity : O(M+N), M and N size of the 2 array +// Space Complexity : O(1), in-place +// Did this code successfully run on Leetcode : Yes +// Approach : +// +// We should compare the last values of the two arrays using 2 pointers, whichever is greater, it should be stored at the last value +// and that pointer should be reduced by 1. +public class MergeSortedArray { + + public void merge(int[] nums1, int m, int[] nums2, int n) { + if(m == 0 && n == 0) return; + int len = m+n-1; + int p1 = m-1; + int p2 = n-1; + while(p1 >= 0 && p2 >=0){ + // Compare the 2 values at last index in the array, copy the greater value to the last index. + // Reduce the pointer by 1 + if(nums1[p1] >= nums2[p2]){ + nums1[len] = nums1[p1]; + len--; + p1--; + }else{ + nums1[len] = nums2[p2]; + len--; + p2--; + } + } + + // Take cares of scenario where p1 reaches 0, indicating all values in nums1 array are explored and nums2 + // array still has some lower values that needs to be explored + while(p2 >= 0){ + nums1[len--] = nums2[p2--]; + } + } +} diff --git a/SearchIn2DMatrix.java b/SearchIn2DMatrix.java new file mode 100644 index 00000000..eecb8a7d --- /dev/null +++ b/SearchIn2DMatrix.java @@ -0,0 +1,36 @@ +// Time Complexity : O(M+N), in the worst case, we will traverse the rows and cols in step form, +// move left, move down, so m rows down, n cols left +// Space Complexity : O(1), in place search +// Did this code successfully run on Leetcode : Yes +// Approach : + +// Brute force - search linearly every cell. +// Optimal - Find a cell which can divide your search space by half (top right corner or bottom left corner) +// From that cell, compare the target, if the target is greater move to the next row as it is sorted and search in that row. +// If the target is smaller, move to prev col and search on the left. +public class SearchIn2DMatrix { + + public boolean searchMatrix(int[][] matrix, int target) { + int rows = matrix.length; + int cols = matrix[0].length; + if(rows == 1 && cols == 1){ + return matrix[0][0] == target; + } + // Search from top left corner + int startRow = 0; + int startCol = cols-1; + + + while(startRow <= rows-1 && startCol >= 0 ){ + if(target == matrix[startRow][startCol]) return true; + // if target is greater, search in next row as values are sorted by row + if(target > matrix[startRow][startCol]){ + startRow++; + }else { + // if target is smaller, search on prev col as values are sorted by col too. + startCol--; + } + } + return false; + } +}