diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..5b1874ee --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,32 @@ + +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +// Use two pointers to scan the array while keeping track of how many times the current number has appeared consecutively. +// If a number appears more than twice, skip it; otherwise, copy it into the correct position. +// Continue this process through the array and return the length of the filtered portion with at most two duplicates. + +public class Solution { + public int removeDuplicates(int[] nums) { + int i =0; + int j =0; + int count = 1; + while (j < nums.length){ + if (j !=0 && nums[j] == nums[j-1]){ + count++; + }else{ + count = 1; + } + + if (count<=2){ + nums[i] = nums[j]; + i++; + } + j++; + } + return i; + } +} diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..c25d2281 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,25 @@ + +# Time Complexity :O(n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :yes +# Three line explanation of solution in plain english + +# Your code here along with comments explaining your approach +# Use two pointers to scan the array while keeping track of how many times the current number has appeared consecutively. +# If a number appears more than twice, skip it; otherwise, copy it into the correct position. +# Continue this process through the array and return the length of the filtered portion with at most two duplicates. + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + slow = 0 + fast = 0 + while fast < len(nums): + if fast != 0 and nums[fast] == nums[fast-1]: + count += 1 + else: + count = 1 + if count <= 2: + nums[slow] = nums[fast] + slow += 1 + fast += 1 + return slow \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..84452487 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,35 @@ + +// Time Complexity : O(m+n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +//Start from the ends of both arrays and compare the largest remaining elements. +// Place the larger value at the end of nums1 and move the corresponding pointer backward. +// After one array is exhausted, copy any leftover elements from nums2 into nums1 since they are already sorted. +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int i = m - 1; // pointer for nums1 + int j = n - 1; // pointer for nums2 + int k = m + n - 1; // pointer for placement in nums1 + + while (i >= 0 && j >= 0) { + if (nums2[j] > nums1[i]) { + nums1[k] = nums2[j]; + j--; + } else { + nums1[k] = nums1[i]; + i--; + } + k--; + } + + // Copy remaining elements from nums2 + while (j >= 0) { + nums1[k] = nums2[j]; + k--; + j--; + } + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..567ee476 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,30 @@ +# Time Complexity : O(m+n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :yes +# Three line explanation of solution in plain english + +# Your code here along with comments explaining your approach +#Start from the ends of both arrays and compare the largest remaining elements. +# Place the larger value at the end of nums1 and move the corresponding pointer backward. +# After one array is exhausted, copy any leftover elements from nums2 into nums1 since they are already sorted. + +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + i = m-1 + j = n-1 + k = m+n-1 + while i >=0 and j >=0: + if nums2[j]>nums1[i]: + nums1[k] =nums2[j] + j -= 1 + else: + nums1[k] = nums1[i] + i -= 1 + k -= 1 + while j >=0: + nums1[k] = nums2[j] + k -= 1 + j -= 1 \ No newline at end of file diff --git a/Problem-3.java b/Problem-3.java new file mode 100644 index 00000000..969f3315 --- /dev/null +++ b/Problem-3.java @@ -0,0 +1,28 @@ + +// Time Complexity :O(mn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +// Start from the top-right corner and compare the target with the current element. +// If the target is smaller, move left; if larger, move down. +// This eliminates one row or one column at each step, ensuring efficient search. +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + int i =0; + int j = n-1; + while (j >=0 && i bool: + i = 0 + j = len(matrix[0])-1 + while i =0: + if target == matrix[i][j]: + return True + elif target < matrix[i][j]: + j -= 1 + else: + i += 1 + return False \ No newline at end of file