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
27 changes: 27 additions & 0 deletions merge-sorted-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity : O(m+n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : We use three pointers: one at the end of the valid elements in nums1 (m - 1), one at the end of nums2 (n - 1), and one at the end of nums1 (m + n - 1).We compare the elements pointed to by nums1 and nums2 and place the larger one at the end of nums1.After placing the element, we move the corresponding pointer backward and continue until all elements from nums2 are merged

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int len = m +n -1;
while(len != -1) {
if(n==0){
break;
}
if(m==0 || nums1[m-1] < nums2[n-1]) {
nums1[len] = nums2[n-1];
n--;
len--;

} else{
nums1[len] = nums1[m-1];
len--;
m--;

}
}
}
}

58 changes: 58 additions & 0 deletions remove-duplicates-from-sorted-array-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity : O(2n) = O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : We use two pointers, slow and fast. The fast pointer moves forward and counts how many times the same digit appears. When the fast pointer encounters a new element or reaches the end of the array, we check the digit count. We take the minimum of the digit count and 2, and then use the slow pointer to update the array. Once the fast pointer reach the end of array we return the slow index.

class Solution {
public int removeDuplicates(int[] nums) {
int slow =0;
int fast =0;
int count =0;
while(fast< nums.length) {
int curr = nums[fast];
while(fast<nums.length && nums[fast] == curr) {
fast++;
count++;
}
int iter = Math.min(count,2);
while(iter !=0) {
nums[slow] = curr;
slow++;
iter --;
}
count =0;
}
return slow;
}
}


// Another approach which works perfectly with same time and space complexity

// int slow = 0;
// int fast = 0;
// int count =1;
// while(fast < nums.length) {
// if(fast == nums.length-1) {
// int iter = Math.min(count, 2);
// for(int i=0; i<iter; i++) {
// nums[slow] = nums[fast];
// slow++;
// }
// break;
// }

// if(nums[fast] == nums[fast+1]) {
// count+=1;
// } else {
// int iter = Math.min(count, 2);
// for(int i=0; i<iter; i++) {
// nums[slow] = nums[fast];
// slow++;
// }
// count =1;
// }
// fast++;
// }
// return slow;
// }
24 changes: 24 additions & 0 deletions search-a-2d-matrix-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time Complexity : O(m+n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english: We start with the right end and if the right end element is equal to target we return it. If the element is greater than target then we go left and if the element is less than target then we go down.

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m =matrix.length;
int n = matrix[0].length;
int row = 0;
int col = n-1;

while(row<m && col> -1) {
if(matrix[row][col] == target) {
return true;
} else if(matrix[row][col] > target) {
col--;
} else {
row++;
}
}
return false;
}
}