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
21 changes: 21 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
80. Remove Duplicates from Sorted Array II
TC: O(n) SC: O(1)
First 2 elements are already valid, put slow and fast on index 2, fast tracks index of new elements to put on index slow, at every point check if the new element on fast is valid by checking if the element has occured has less than 2 times by checking slow - k, this works since array is sorted, since if the element has occured less than 2 times slow - k will not be equal to element on index fast.
*/
class Solution {
public int removeDuplicates(int[] nums) {
int k = 2; // Max 2 duplicates are allowed
int slow = k, fast = k; // First 2 elements in nums are always valid

for(int i = k; i < nums.length; i++){
if(nums[slow - k] != nums[fast]){ // Check if the element has not occured more than k times
nums[slow] = nums[fast]; // Store it
slow++;
}
fast++; // Move to next element
}

return slow;
}
}
30 changes: 30 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
88. Merge Sorted Array
Ran on leetcode: Yes
TC: O(m + n) SC: O(1)
Start at last index of nums1, nums2 and result, since that would avoid loss of values of nums1. Check if element in nums1 is greater than nums2 and place in result and vice versa. Fill rest of the element remaining in nums2 if necessary.
*/
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {

int p1 = m - 1, p2 = n - 1, r1 = m + n - 1; // Pointer on last index of nums1, nums2 and result array


while(p1 >= 0 && p2 >= 0){ // Fill elements from end of result array, to not loose elements from nums1
if(nums1[p1] > nums2[p2]){ // If the element on nums1 is greater than nums2 place nums1 element
nums1[r1] = nums1[p1]; // Place on last unfilled index of result
p1--;
} else { // If the element on nums2 is greater than nums1 place nums1 element
nums1[r1] = nums2[p2]; // Place on last unfilled index of result
p2--;
}
r1--;
}

while(p2 >= 0){ // Fill rest of the elements of nums2, since nums1 is result the element in nums1 are already at right place
nums1[r1] = nums2[p2];
p2--;
r1--;
}
}
}
25 changes: 25 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
240. Search a 2D Matrix II
Ran on leetcode: Yes
TC: O(m + n) SC: O(1)
Since the row and column are both sorted, we can start at top right corner, move down if the target is greater than current element and move left if the target is smaller until we find the target or return false.
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {

int cLen = matrix[0].length, rLen = matrix.length; // Dimensions of matrix
int c = cLen - 1, r = 0; // Top - Right column index

while(r < rLen && c >= 0){ // Index is valid
if(matrix[r][c] == target){ // Target found
return true;
} else if(matrix[r][c] > target){ // Element is greater than target
c--; // Move left
} else { // Element is smaller than target
r++; // Move down
}
}

return false; // Element not found
}
}