diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Two-Pointers-2.iml b/.idea/Two-Pointers-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Two-Pointers-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..827d064a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MergeSortedArrays.java b/MergeSortedArrays.java new file mode 100644 index 00000000..a0279a1e --- /dev/null +++ b/MergeSortedArrays.java @@ -0,0 +1,30 @@ +import java.util.Arrays; +public class MergeSortedArrays { + public void merge(int[] nums1, int m, int[] nums2, int n){ + int i = m - 1; + int j = n - 1; + int k = m + n - 1; + while(i >= 0 && j >= 0){ + if(nums1[i] >= nums2[j]){ + nums1[k] = nums1[i]; + k--; + i--; + }else{ + nums1[k] = nums2[j]; + k--; + j--; + } + } + } + public static void main(String[] args){ + MergeSortedArrays solution = new MergeSortedArrays(); + + int [] nums1 = {1,2,3,0,0,0}; int m = 3; + int [] nums2 = {2,5,6}; int n = 3; + solution.merge(nums1, m, nums2, n); + System.out.println(Arrays.toString(nums1)); + } +} + +//Time complexity : O(m+n) +//Space complexity : O(1) \ No newline at end of file diff --git a/RemoveDuplicates.java b/RemoveDuplicates.java new file mode 100644 index 00000000..656cb3d1 --- /dev/null +++ b/RemoveDuplicates.java @@ -0,0 +1,19 @@ +public class RemoveDuplicates { + public static int removeDuplicates(int[] nums) { + int i = 0; + for(int n: nums){ + if(i<2 || n!=nums[i-2]){ + nums[i++]=n; + } + } + return i; + } + public static void main(String[] args){ + int[] nums={1,1,1,2,2,3,3,4}; + System.out.println(removeDuplicates(nums)); + } +} + + +//Time complexity : O(n) +//Space complexity : O(1) \ No newline at end of file diff --git a/SearchMatrix.java b/SearchMatrix.java new file mode 100644 index 00000000..7afeea61 --- /dev/null +++ b/SearchMatrix.java @@ -0,0 +1,23 @@ +public class SearchMatrix { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + int i = 0; + int j = n - 1; + while(i < m && j >= 0 ){ + if(matrix[i][j] == target) return true; + else if(matrix[i][j] > target){ + j--; + } else{ + i++; + } + } + return false; + } +} + +//TC :Time Complexity: +//O(m log n) + +//Space Complexity: +//O(1) \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/.gitignore b/out/production/Two-Pointers-2/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Two-Pointers-2/.idea/Two-Pointers-2.iml b/out/production/Two-Pointers-2/.idea/Two-Pointers-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/Two-Pointers-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/codeStyles/Project.xml b/out/production/Two-Pointers-2/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/codeStyles/codeStyleConfig.xml b/out/production/Two-Pointers-2/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/misc.xml b/out/production/Two-Pointers-2/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/modules.xml b/out/production/Two-Pointers-2/.idea/modules.xml new file mode 100644 index 00000000..827d064a --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/.idea/vcs.xml b/out/production/Two-Pointers-2/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Two-Pointers-2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-2/MergeSortedArrays.class b/out/production/Two-Pointers-2/MergeSortedArrays.class new file mode 100644 index 00000000..8704db39 Binary files /dev/null and b/out/production/Two-Pointers-2/MergeSortedArrays.class differ diff --git a/out/production/Two-Pointers-2/README.md b/out/production/Two-Pointers-2/README.md new file mode 100644 index 00000000..e1a6f9d6 --- /dev/null +++ b/out/production/Two-Pointers-2/README.md @@ -0,0 +1,10 @@ +# Two-Pointers-2 + +## Problem1 (https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) + + +## Problem2 (https://leetcode.com/problems/merge-sorted-array/) + + +## Problem3 (https://leetcode.com/problems/search-a-2d-matrix-ii/) + diff --git a/out/production/Two-Pointers-2/RemoveDuplicates.class b/out/production/Two-Pointers-2/RemoveDuplicates.class new file mode 100644 index 00000000..ad2180e8 Binary files /dev/null and b/out/production/Two-Pointers-2/RemoveDuplicates.class differ