diff --git a/problem37.py b/problem37.py new file mode 100644 index 00000000..7ef8a918 --- /dev/null +++ b/problem37.py @@ -0,0 +1,37 @@ +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. + """ + first = m-1 + second = n-1 + curr = len(nums1)-1 + + while first >= 0 and second >= 0:#stop when either array is exhausted + if nums1[first] > nums2[second]: + nums1[curr] = nums1[first] + first -= 1 + curr -= 1 + else: + nums1[curr] = nums2[second] + second -= 1 + curr -= 1 + + if first < 0:#first array got exhausted, then copy second to first. Meaning we processed all elements in first array to their correct position + while second >= 0: + nums1[curr] = nums2[second] + curr -= 1 + second -=1 + + #if second array got exhausted do nothing. as it will be correctly placed to the end of the nums1 array. + + +#if we start from the begining of array, there is a chance we may lose the sorting +#[5 6 7 0 0 0] [2 3 4] -> second array can become [5 3 4], we are losing sorting +# so start from where there is no issue of overwriting. we won't lose sorted order. so +# collect and put to the last. Collecting pointer from last. +# Collecting point shud start from the end, if it stars from the begining the we can lose sorting. +# comparing pointers start from the end as well bcuz bigger no can go to the end. + +# Time Complexity: O(m+n) +# Space Complexity: O(1) \ No newline at end of file diff --git a/problem38.py b/problem38.py new file mode 100644 index 00000000..ec55aca6 --- /dev/null +++ b/problem38.py @@ -0,0 +1,33 @@ +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + #here ful 2D matrix is not sorted. Only rows are sorted. + #so can't consider as a 1-D array. + #we use steps method O(m+n) + #can do BS on rows O(m*logn) + #can do BS on column O(n*logm) + #brute force is O(m*n) + + #in steps method start from top right or bottom left cell. + #other two corners we can't make a decision in which way to proceed as both sides will be either smaller or greater + + #starting from top right corner here + + m = len(matrix) + n = len(matrix[0]) + + #right most corner, for left bottom take accordingly + row = 0 + col = n-1 + + while 0 <= row < m and 0 <= col < n: + if target == matrix[row][col]: + return True + elif target > matrix[row][col]: + row += 1 + else: + col -= 1 + + return False + + # Time Complexity: O(m+n) + # Space Complexity : O(1) \ No newline at end of file diff --git a/problem39.py b/problem39.py new file mode 100644 index 00000000..8aba5882 --- /dev/null +++ b/problem39.py @@ -0,0 +1,31 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + #brute force solution + #create a freq map, then overwrite the array but O(n) space + + slow = 0 #this is to re-write the array + fast = 0 #this is to iterate over the array + + count = 0 + + while fast < len(nums): + + curr = nums[fast] + + while (fast < len(nums) and nums[fast] == curr): + fast += 1 + count += 1 + + jumps = min(count, 2) + + while jumps > 0: + nums[slow] = curr + slow += 1 + jumps -= 1 + + count = 0 + + return slow + + # Time Complexity: O(2N) = O(N) + # Space Complexity: O(1) \ No newline at end of file