Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions merge.java
Original file line number Diff line number Diff line change
@@ -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<m+n; i++){
nums1[i] = nums2[j];
j++;
}
Arrays.sort(nums1);
}
}
25 changes: 25 additions & 0 deletions removeDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(2n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None
class Solution {
public int removeDuplicates(int[] nums) {
int k = 2;
List<Integer> result = new ArrayList<>();

Map<Integer, Integer> 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();
}
}
20 changes: 20 additions & 0 deletions searchMatrix.java
Original file line number Diff line number Diff line change
@@ -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;
}
}