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
38 changes: 38 additions & 0 deletions merge_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# https://leetcode.com/problems/merge-sorted-array/

# Using two pointers started updenting the elements from second array into the first array. While doing that, compare the last element of the first array with last element of second array and update accordinlgy

# Time Complexity- O((m+n) Space Complexity- O(1)
class Solution:
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
# finding the last index of the merge array
last_index= m + n - 1

# merge from last index
while m > 0 and n > 0:
# checking whether the last element of first array is greater than the last element of second array
if nums1[m - 1] > nums2[n - 1]:
# update the last index of the nums1 with greater element
nums1[last_index] = nums1[m - 1]
# decrement the index
m -= 1
# when the last element of second array is greater than the last element of first array
else:
nums1[last_index] = nums2[n - 1]
# decrement the index
n -= 1
# decrement the last index
last_index -= 1

# update the remaining elements from second array into the first array
# check if n is greater than 0 after decrementing
while n > 0:
# update with the elements
nums1[last_index] = nums2[n-1]
# decrement the pointer
n = n - 1
last_index = last_index - 1

solution = Solution()
print(solution.merge([1,2,3,0,0,0],3,[2,5,6],3))

33 changes: 33 additions & 0 deletions remove_duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

# Time Complexity- O(n) Space Complexity- O(1)
class Solution:
def removeDuplicates(self, nums: list[int]) -> int:
k = 2
old = 0
new = 0
count = 0
while new < len(nums):
# check whether the previous number is same as the current
if new != 0 and nums[new] == nums[new - 1]:
# increase the count
count += 1
else:
# if the first occurance
count = 1
# check the count is in given limit
if count <= k:
# update the list with old
nums[old] = nums[new]
# increment the old
old += 1
# increment the new
new += 1
# return the old
return old


solution = Solution()
print(solution.removeDuplicates([1,1,1,2,2,3]))
# nums = [1,1,1,2,2,3]
# output = 5
30 changes: 30 additions & 0 deletions search_a_2d_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# https://leetcode.com/problems/search-a-2d-matrix-ii/

# To find the target element, start checking from the top left corner of the matrix. If the target is less than the current element decrease the column as the matrix is sorted. and if it's greater than the current element increase the row.
# Time Complexity- O(m + n) Space Complexity- O(1)
class Solution:
def searchMatrix(self, matrix: list[list[int]], target: int) -> bool:
m = len(matrix)
n = len(matrix[0])
# starting from the top-right corner
r, c = 0 , n - 1

while r < m and c >= 0:
# if the target is present at this coordinate
if matrix[r][c] == target:
return True
# if the target is less than the element
elif matrix[r][c] > target:
# decrement the column
c -= 1
else:
# increment the row
r += 1

return False

solution = Solution()
print(solution.searchMatrix([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], 19))

# target = 5
# output = true