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
33 changes: 33 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
/*
Use a slow pointer to build the final array while scanning with i.
Track how many times the current number has appeared; only allow it if count ≤ 2.
Whenever allowed, place the element at slow, ensuring every number appears at most twice.
*/

public class Problem1 {
public int removeDuplicates(int[] nums) {
int slow = 1;
int cnt = 1;

for(int i = 1;i<nums.length;i++) {
if(nums[i] == nums[i-1]) {
cnt++;
} else {
cnt = 1;
}

if(cnt <= 2) {
nums[slow] = nums[i];
slow++;
}
}

return slow;
}
}
38 changes: 38 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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

// Your code here along with comments explaining your approach
/*
Start three pointers from the end: p1 at last real element of nums1, p2 at last of nums2, and p3 at the very end of nums1.
Compare nums1[p1] and nums2[p2], and put the larger one at nums1[p3], moving the corresponding pointer(s) left.
If anything remains in nums2, copy it into the front of nums1 (leftover elements in nums1 are already in correct position).
*/

public class Problem2 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m-1;
int p2 = n-1;
int p3 = (m+n) - 1;

while(p1 >= 0 && p2 >= 0) {
if(nums2[p2] > nums1[p1]) {
nums1[p3] = nums2[p2];
p2--;
} else {
nums1[p3] = nums1[p1];
p1--;
}

p3--;
}

while(p2 >= 0) {
nums1[p3] = nums2[p2];
p2--;
p3--;
}

}
}
31 changes: 31 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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

// Your code here along with comments explaining your approach
/*
I will follow a stair-case pattern, I will start at the first row and last column. Since the property of sorted matrix exists.
If the current element is greater than the target that means there is a possiblity that element exists in the left side so decrease the column by 1.
If the current element is smaller. It means that there is a possibility that elements exists in the below, so increase the row by 1.
*/

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

while(row >= 0 && row < matrix.length && col >= 0 && col < matrix[0].length) {
int element = matrix[row][col];
if(element == target) {
return true;
} else if(element > target) {
col--;
} else {
row++;
}
}

return false;
}
}