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
28 changes: 28 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to allow only 2 duplicates and shift in-place.


// Your code here along with comments explaining your approach in three sentences only
// Since array is sorted, same numbers come together, so we can control how many times we keep them.
// I use a write pointer and allow a number only if it is different from nums[write-2] (means max 2 allowed).
// This updates the array in-place and returns the new length.

class Solution {
public int removeDuplicates(int[] nums) {

if (nums.length <= 2) return nums.length;

int write = 2;

for (int i = 2; i < nums.length; i++) {
if (nums[i] != nums[write - 2]) {
nums[write] = nums[i];
write++;
}
}

return write;
}
}
30 changes: 30 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(m + n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to merge from end to avoid overwriting nums1.


// Your code here along with comments explaining your approach in three sentences only
// Since nums1 has extra space at the end, I fill it from the back so existing values don't get overwritten.
// I compare nums1[m-1] and nums2[n-1] and place the bigger one at the end pointer.
// Continue until nums2 is fully placed, because remaining nums1 part is already in correct position.

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 (j >= 0) {
if (i >= 0 && nums1[i] > nums2[j]) {
nums1[k] = nums1[i];
i--;
} else {
nums1[k] = nums2[j];
j--;
}
k--;
}
}
}
30 changes: 30 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(m + n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to start from correct corner.


// Your code here along with comments explaining your approach in three sentences only
// I start from the top-right corner because from there I can eliminate one full row or column each step.
// If the current value is bigger than target, I move left, and if smaller, I move down.
// This way I never revisit cells and finish in O(m+n).

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {

int m = matrix.length;
int n = matrix[0].length;

int r = 0;
int c = n - 1;

while (r < m && c >= 0) {

if (matrix[r][c] == target) return true;
else if (matrix[r][c] > target) c--;
else r++;
}

return false;
}
}