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
29 changes: 29 additions & 0 deletions MergeSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://leetcode.com/problems/merge-sorted-array/description
// Time complexity : O(m+n) where m is number of elements in num1 and n is number of elements in num2
// Space complexity : O(1)
// Did this code successfully run on Leetcode : yes
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m-1;
int j = n-1;
int k = m+n-1;
while(i>=0 && j>=0){
int val1 = nums1[i];
int val2 = nums2[j];
if(val1>val2){
nums1[k] = val1;
i--;
k--;
}else{
nums1[k] = val2;
j--;
k--;
}
}
while(j>=0){
nums1[k] = nums2[j];
j--;
k--;
}
}
}
27 changes: 27 additions & 0 deletions Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,30 @@
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
// Time complexity :O(n)
// Space complexity : O(1)
// Did this code successfully run on Leetcode : yes
class Solution {

public int removeDuplicates(int[] nums) {
int i = 1;
int count = 1;
int j = 1;
while(j<nums.length){
if(nums[j]==nums[j-1]){
count++;
if(count>2){
j++;
continue;
}
}else{
count = 1;
}
nums[i] = nums[j];
i++;
j++;
}
return i;
}
}
21 changes: 21 additions & 0 deletions Search2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://leetcode.com/problems/search-a-2d-matrix-ii/
// Time complexity : O(m+n) where m is number of rows and n is number of columns
// Space complexity : O(1)
// Did this code successfully run on Leetcode : yes
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row = matrix.length;
int col = matrix[0].length;
int i = 0;
int j = col-1;
while(i<=row-1 && j>=0){
if(matrix[i][j]==target) return true;
else if(matrix[i][j]>target){
j--;
}else{
i++;
}
}
return false;
}
}