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
44 changes: 44 additions & 0 deletions mergeSortedArrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Problem: Merge Sorted Array
----------------------------
You are given two sorted integer arrays nums1 and nums2, and two integers m and n.
Merge nums2 into nums1 as one sorted array in-place.

Approach (One-liner):
Use three pointers starting from the end of both arrays to fill nums1 from the back.

Time Complexity: O(m + n)
Space Complexity: O(1) — in-place merge without using extra space
"""

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.
"""

# Start filling from the last position of nums1 (where the largest element should go)
write_index = m + n - 1

# Last valid elements in nums1 and nums2
i = m - 1 # pointer for nums1
j = n - 1 # pointer for nums2

# Merge nums1 and nums2 from the back
while i >= 0 and j >= 0:
# Compare the last unmerged elements in nums1 and nums2
# Place the larger one at the current write_index position
if nums1[i] > nums2[j]:
nums1[write_index] = nums1[i]
i -= 1
else:
nums1[write_index] = nums2[j]
j -= 1
write_index -= 1

# If any elements remain in nums2, copy them into nums1
# (If nums1 still has remaining elements, they’re already in place)
while j >= 0:
nums1[write_index] = nums2[j]
j -= 1
write_index -= 1
37 changes: 37 additions & 0 deletions removeDuplicatesInPlace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
Two-pointer approach: Use 'slow' pointer to track position for valid elements,
and 'i' pointer to scan through array, keeping count of consecutive duplicates.
Time Complexity: O(n) - single pass through the array
Space Complexity: O(1) - only using constant extra space (pointers and counter)
"""

k = 2 # Maximum allowed occurrences for each unique element

# Start slow pointer at index 1 (first element at index 0 is always valid)
slow = 1

# Track consecutive occurrences of current element (starts at 1 for nums[0])
count = 1

# Iterate through array starting from index 1
for i in range(1, len(nums)):

if nums[i] == nums[i-1]:
# Current element equals previous element - increment occurrence count
count += 1
else:
# New element encountered - reset count to 1
count = 1

# Only include element if it hasn't exceeded max allowed occurrences (k)
if count <= k:
# Place current element at slow pointer position
nums[slow] = nums[i]
# Move slow pointer forward to next available position
slow += 1
# If count > k, skip this element (don't advance slow pointer)

# Return the length of the modified array (slow pointer is at next empty position)
return sloww the e
47 changes: 47 additions & 0 deletions search2DMatrixTwo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Problem: Search a 2D Matrix II
------------------------------
Write an efficient algorithm that searches for a target value in an m x n matrix.
Each row is sorted in ascending order from left to right, and each column is sorted
in ascending order from top to bottom.

Approach (One-liner):
Start from the top-right corner and eliminate one row or one column in each step.

Time Complexity: O(m + n)
Space Complexity: O(1) — constant extra space
"""

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
"""
Searches for 'target' in a sorted 2D matrix using an efficient elimination method.
"""

# Number of rows (m) and columns (n)
m = len(matrix)
n = len(matrix[0])

# Start at the top-right corner
row = 0
col = n - 1

# Traverse the matrix until we move out of bounds
while row < m and col >= 0:

# If the current element matches the target, we found it
if matrix[row][col] == target:
return True

# If current element is greater than target,
# move left to find smaller values
elif matrix[row][col] > target:
col -= 1

# If current element is smaller than target,
# move down to find larger values
else:
row += 1

# If we exit the loop, the target does not exist in the matrix
return False