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
27 changes: 27 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Remove Duplicates from Sorted Array II

# Time comlexity -> O(n)
# Space complexity -> O1
# Logic -> maitain 2 pointers, slow and fast, fast will take care of occurences, for 1st 2 ocurences it will update the slow with fast value
# once occurences move above 2 it's just on keeps moving ahead to find new number that needs to be moved in place of slow pointer's value

from typing import List
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow = 0
fast = 0
length = len(nums)
count=0
k = 2
for fast in range(0,length):
if (fast != 0 and nums[fast]==nums[fast-1]):
count+=1
else:
count = 1

if count <=k:
nums[slow]=nums[fast]
slow+=1

return slow

33 changes: 33 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#Merge Sorted Array
# Time complexity O(m+n)
# Space complexity -> O1
# Logic -> Start from updating the biggest elemnt in the correct place this way when we overwrite the bigger items
# with the smaller items bigger items won't be lost as they will already be copied in the correct position

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.
"""
smallListPointer = n-1
bigListpointer = m-1
finalListPOinter = len(nums1)-1

while smallListPointer >=0 and bigListpointer>=0:
nums1item = nums1[bigListpointer]
nums2Item = nums2[smallListPointer]
if nums1item > nums2Item:
nums1[finalListPOinter] = nums1item
bigListpointer-=1
else:
nums1[finalListPOinter] = nums2Item
smallListPointer-=1
finalListPOinter-=1

if bigListpointer == -1:
while finalListPOinter >= 0:
nums1[finalListPOinter] = nums2[smallListPointer]
finalListPOinter-=1
smallListPointer-=1


30 changes: 30 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#Search a 2D Matrix II

# Time complexity O(m+n) where m is rows and n is columns
# Space complexity -> O1
# Logic -> start from last row 1st column. based on the target comparison we can move up if target is lower than cuurrnt element if bigger move right
# keep on moving until you find element or you cross the matrix boundary which means target is not there

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
startRow = len(matrix)-1
startColumn = 0
maxColumns = len(matrix[0])

while startRow>=0 and startColumn<maxColumns:
# if current number is target return true
currentItem = matrix[startRow][startColumn]
if currentItem == target:
return True

#if target is smaller than current matrix move up because all other items in row will ve big
if target < currentItem:
startRow-=1

#if target is greate than current matrix move right because all other items in column above will be smaller
if target > currentItem:
startColumn+=1

return False