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
93 changes: 93 additions & 0 deletions TW2_240_Search_a_2D_Matrix_II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**** Method 1 ****/
//Time Complexity: O(n*m)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

//Use two for loops to traverse and search for element, if found return true else false.

/**** Method 2 ****/
//Time Complexity: O(nLogm)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

// Rather doing a linear search, we can use binary search as the all rows are sorted, so start traversing the matrix row wise and check if the target might be present in that row or not. If it might then do binarySearch on that row. If a target is found anywhere return true else false at the end.

/**** Method 2 ****/
//Time Complexity: O(Log(m+n))
//Space Complexity: O(1)

//Successfully submitted in LeetCode

// If we observe, all elements are sorted row and column-wise, so we start at right most corner of the matrix and check if it is the target, if not then if it less than target we can move in the column i.e i++, else if greater than target we can move row wise i.e j--. If at any point i or j crosses it boundaries, then return false.

public class TW2_240_Search_a_2D_Matrix_II {

/**** Method 1 ****/
public boolean searchMatrix1(int[][] matrix, int target) {
for (int[] arr : matrix) {
for (int i : arr) {
if (i == target) {
return true;
}
}
}
return false;
}

/**** Method 2 ****/
public boolean searchMatrix2(int[][] matrix, int target) {
int n = matrix.length;
int m = matrix[0].length;

for (int i = 0; i < n; i++) {
if (matrix[i][0] <= target && matrix[i][m - 1] >= target) {
if (binarySearch(matrix[i], target)) {
return true;
}
}
}

return false;
}

public boolean binarySearch(int arr[], int target) {
int lo = 0;
int hi = arr.length - 1;

while (lo <= hi) {
int mid = lo + (hi - lo) / 2;

if (arr[mid] == target) {
return true;
} else if (arr[mid] < target) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}

return false;
}

/**** Method 3 ****/
public boolean searchMatrix3(int[][] matrix, int target) {
int n = matrix.length;
int m = matrix[0].length;
int i = 0;
int j = m - 1;

while (i < n && j >= 0) {
if (matrix[i][j] == target) {
return true;
} else if (matrix[i][j] < target) {
i++;
} else {
j--;
}
}

return false;
}
}
65 changes: 65 additions & 0 deletions TW2_80_Remove_Duplicates_from_Sorted_Array_II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**** Method 1 ****/
//Time Complexity: O(2n)
//Space Complexity: O(n)

//TLE when submitted in LeetCode

//Take a HashMap to store the frequency of each element, while storing the count, if the count is less than 2 then add to the list, then copy the elements from list to array and return the size of list.

/**** Method 2 ****/
//Time Complexity: O(n^2)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

// Take two pointers and a count variable, if the current element is same as previous then increment the count, else make count 1, if count is less than or equal to 2 then copy the element to the slow pointer and increment the slow pointer, finally return the slow pointer.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TW2_80_Remove_Duplicates_from_Sorted_Array_II {

/**** Method 1 ****/
public int removeDuplicates1(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();

for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);

if (map.get(i) < 2) {
list.add(i);
}
}

for (int i = 0; i < list.size(); i++) {
nums[i] = list.get(i);
}

return list.size();
}

/**** Method 2 ****/
public int removeDuplicates2(int[] nums) {
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 <= 2) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}

return slow;
}
}
52 changes: 52 additions & 0 deletions TW2_88_Merge_Sorted_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**** Method 1 ****/
//Time Complexity: O((m+n)log(m+n))
//Space Complexity: O(n)

//Successfully when submitted in LeetCode

//Copy all the elements from arr2 to arr1 end and then sort the arr1, to get a completely sorted arr of both arr1 and arr2.

/**** Method 2 ****/
//Time Complexity: O(m+n)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

// Two pointers, take two pointer starting from end of both array and now take another third pointer which points to end of arr1 i.e m + n - 1, now populate the third pointer with largest value of i and j pointer and decrement, so on continue till j >=0.

import java.util.Arrays;

public class TW2_88_Merge_Sorted_Array {

/**** Method 1 ****/
public void merge1(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1;
int j = 0;

while (j < n) {
nums1[i + 1] = nums2[j];
i++;
j++;
}
Arrays.sort(nums1);
}

/**** Method 2 ****/
public void merge2(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--;
}
}
}