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
24 changes: 24 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 jumps=Math.min(count,2);

while(jumps!=0){
nums[slow]=curr;
slow++;
jumps--;
}
count=0;
}
return slow;
}
}
30 changes: 30 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {

int p1=m-1;
int p2=n-1;
int idx=m+n-1;
while(p1>=0 && p2>=0){
//if value at pointer1 is greater than value in 2nd array of pointer 2
//then we put that value at 1st array at last idx
if(nums1[p1]>=nums2[p2]){
nums1[idx]=nums1[p1];
p1--;
}
else{
//if value at pointer2 is greater than value in 1st array of pointer 1
//then we put that value at 1st array at last idx
nums1[idx]=nums2[p2];
p2--;
}
//move the idx pointer inwards
idx--;
}
//if p2 is not out of bonds, just copy the elements from p2 to idx pointer
while(p2>=0){
nums1[idx]=nums2[p2];
p2--;
idx--;
}
}
}
21 changes: 21 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n= matrix[0].length;
//traversing through top-right corner
int i=0; int j=n-1;
while(i<m && j>=0){

if(matrix[i][j]==target) return true;
//traverse to left columns
else if(matrix[i][j]>target){
j--;
}
else{
//traverse down rows
i++;
}
}
return false;
}
}