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
24 changes: 24 additions & 0 deletions Problem1_RemoveDuplicatesFromSortedArrayII.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Time Complexity : O(n) where n is the length of the array
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Use two pointers where slow pointer tracks position to place next valid element.
# Fast pointer traverses array, and we allow at most 2 occurrences by checking if current element equals element at slow-2.
# Only place element at slow pointer if it doesn't create more than 2 duplicates.

class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) <= 2:
return len(nums)

slow = 2 # Start from index 2 since first two elements are always valid

for fast in range(2, len(nums)):
# If current element is different from element at slow-2, it's safe to place
# This ensures at most 2 occurrences of any element
if nums[fast] != nums[slow - 2]:
nums[slow] = nums[fast]
slow += 1

return slow
34 changes: 34 additions & 0 deletions Problem2_MergeSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Time Complexity : O(m + n) where m and n are lengths of nums1 and nums2
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Use two pointers starting from the end of both arrays, comparing and placing larger elements at the end of nums1.
# Start from the last valid positions (m-1 for nums1, n-1 for nums2) and work backwards.
# This avoids overwriting unprocessed elements in nums1 since we're filling from the end.

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.
"""
# Pointers for nums1, nums2, and the end of merged array
i = m - 1
j = n - 1
k = m + n - 1

# Merge from the end
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[k] = nums1[i]
i -= 1
else:
nums1[k] = nums2[j]
j -= 1
k -= 1

# Copy remaining elements from nums2 if any
while j >= 0:
nums1[k] = nums2[j]
j -= 1
k -= 1
32 changes: 32 additions & 0 deletions Problem3_Search2DMatrixII.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time Complexity : O(m + n) where m is rows and n is columns
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Start from top-right corner (or bottom-left) and use two pointers to traverse.
# If current element is greater than target, move left (eliminate column); if smaller, move down (eliminate row).
# This leverages the sorted property of both rows and columns to efficiently narrow down the search space.

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix or not matrix[0]:
return False

m, n = len(matrix), len(matrix[0])
# Start from top-right corner
row = 0
col = n - 1

while row < m and col >= 0:
current = matrix[row][col]

if current == target:
return True
elif current > target:
# Current element is too large, eliminate this column
col -= 1
else:
# Current element is too small, eliminate this row
row += 1

return False