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
23 changes: 23 additions & 0 deletions Leetcode240.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions Leetcode80.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions Leetcode88.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}