diff --git a/Leetcode240.java b/Leetcode240.java new file mode 100644 index 00000000..b5b69fe0 --- /dev/null +++ b/Leetcode240.java @@ -0,0 +1,23 @@ +// TC - O(m*n) +// SC - O(1) +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + int i = m - 1; // row + int j = 0; // col + // Starting from the left bottom most index so that everything towards right is + // in increasing order and everything upwards is in decreasing order. This will + // help in eliminating one section while searching. + while (i < m && j < n && i >= 0 && j >= 0) { // Keep the row and col values within range + if (matrix[i][j] == target) + return true; + if (matrix[i][j] < target) { // + j++; + } else { + i--; + } + } + return false; + } +} \ No newline at end of file diff --git a/Leetcode80.java b/Leetcode80.java new file mode 100644 index 00000000..015b15d5 --- /dev/null +++ b/Leetcode80.java @@ -0,0 +1,28 @@ +// TC - O(n) +// SC - O(1) +class Solution { + public int removeDuplicates(int[] nums) { + int slow = 0; + int fast = 0; + int count = 0; + int n = nums.length; + int k =2; + while(fast< n) { + + if(fast != 0 && nums[fast]== nums[fast-1]) { + count+=1; + } else { + count =1; + } + + if(count<=k) { + nums[slow] = nums[fast]; + slow++; + } + fast+=1; + + + } + return slow; + } +} \ No newline at end of file diff --git a/Leetcode88.java b/Leetcode88.java new file mode 100644 index 00000000..4ffcdb3b --- /dev/null +++ b/Leetcode88.java @@ -0,0 +1,28 @@ +// TC - O(m+n) +// SC - O(1) +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int first = m - 1; + int second = n - 1; + int i = m + n - 1; + while (first >= 0 && second >= 0) { // compare the highest element in each array and add them to the right most + // element on nums1 array + if (nums1[first] > nums2[second]) { + nums1[i] = nums1[first]; + first--; + } else { + nums1[i] = nums2[second]; + second--; + } + i -= 1; + } + while (second >= 0) { // if the nums2 array still has items left in it after the above iteration, we + // can directly copy them into nums1 array via the i'th pointer. We don't need + // to do this for num1 array elements since they are already in place incase + // they have been left out. + nums1[i] = nums2[second]; + second--; + i -= 1; + } + } +} \ No newline at end of file