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
22 changes: 22 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Time Complexity --> O(n) where n is the size of array
# Space Complexity --> O(1)
# Approach --> Use slow and fast pointers where the fast pointer keeps check on number of duplicates. If the count is more than 2, we reset the count and irrespective of the conditions the fast pointer moves.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums)<=1:
return len(nums)
slow = 1
fast = 1
count = 1
while fast<len(nums):
if nums[fast]==nums[fast-1]:
count += 1
if count<=2:
nums[slow] = nums[fast]
slow += 1
else:
count = 1
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
21 changes: 21 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time Complexity --> O(m+n) where m and n are the number of elements from both arrays
# Space Complexity --> O(1)
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.
"""
p1 = m-1
p2 = n-1
d = m+n-1
while p1>=0 and p2>=0:
if nums1[p1]>=nums2[p2]:
nums1[d] = nums1[p1]
p1 = p1-1
else:
nums1[d] = nums2[p2]
p2 = p2-1
d = d-1
while p2>=0:
nums1[p2] = nums2[p2]
p2 = p2-1
15 changes: 15 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Time Complexity --> O(m+n) where m, n are the dimensions of matrix
# Space Complexity --> O(1)
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
r = len(matrix)-1
c = 0

while r>=0 and c<len(matrix[0]):
if matrix[r][c]==target:
return True
elif matrix[r][c] < target:
c = c+1
else:
r = r-1
return False