diff --git a/merge.java b/merge.java new file mode 100644 index 00000000..3c2158d4 --- /dev/null +++ b/merge.java @@ -0,0 +1,14 @@ +// Time Complexity : O((m + n) log(m + n)) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int j = 0; + for(int i=m; i result = new ArrayList<>(); + + Map map = new HashMap<>(); + + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + if (map.get(num) <= k) { + result.add(num); + } + } + + for (int i = 0; i < result.size(); i++) { + nums[i] = result.get(i); + } + + return result.size(); + } +} diff --git a/searchMatrix.java b/searchMatrix.java new file mode 100644 index 00000000..eee0ac11 --- /dev/null +++ b/searchMatrix.java @@ -0,0 +1,20 @@ +// Time Complexity : O(m+n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + + int r = m - 1, c = 0; + + while(r >= 0 && c < n){ + if(matrix[r][c] == target) return true; + else if(matrix[r][c] > target) r--; + else c++; + } + + return false; + } +} \ No newline at end of file