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
30 changes: 30 additions & 0 deletions Problem37.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


// Your code here along with comments explaining your approach
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 (nums2[p2] > nums1[p1]) {
nums1[idx] = nums2[p2];
p2--;
} else {
nums1[idx] = nums1[p1];
p1--;
}
idx--;
}

while(p2 >= 0) {
nums1[idx] = nums2[p2];
p2--;
idx--;
}
}
}
23 changes: 23 additions & 0 deletions Problem38.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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


// Your code here along with comments explaining your approach
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;
}
}
32 changes: 32 additions & 0 deletions Problem39.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
class Solution {
public int removeDuplicates(int[] nums) {
int k = 2;
int slow = 0; int fast = 0;

int count = 0;

while (fast < nums.length) {
if (fast != 0 && nums[fast] == nums[fast-1]) {
count++;
} else {
count = 1;
}

if (count <= k) {
nums[slow] = nums[fast];
slow++;
}

fast++;
}

return slow;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.