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
33 changes: 33 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#Sort Colors

#O(n) time complexity
#O(1) space complecity
#Logic -> Have 3 ointers, left, mid and right. if mid is 2, replace with right pointer and move right pointer by 1 on left.
# If mid is 1 move mid pointer by 1 to right. If mid is 0 replace it with left and move both left and pointers
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
leftPointer = 0
midPointer = 0
rightPointer = len(nums)-1

while midPointer <=rightPointer:
numleft = nums[leftPointer]
numRight = nums[rightPointer]
numMid = nums[midPointer]
if numMid == 2:
nums[midPointer] = numRight
nums[rightPointer] = numMid
rightPointer-=1
elif numMid == 1:
midPointer+=1
else:
nums[midPointer] = numleft
nums[leftPointer] = numMid
leftPointer+=1
midPointer+=1



42 changes: 42 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#3Sum
# Time complexity -> On^2)
# Space complexity -> O1
# Sort the nums, the for each element use 2sum logic to find the numbers which will be equal to -1*element basically then -vs of the element will become
# the target for the 2sum

# from typing import List
class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
result=[]
nums.sort()
for i in range(0,len(nums)):
num1 = nums[i]
if i>0 and nums[i]==nums[i-1]:
continue
self.twoSum(nums,i+1,num1*-1,result)
return result


def twoSum(self,nums,startingIndex,target,result):
# subResult = set()
leftPointer = startingIndex
rightPointer = len(nums)-1
while leftPointer < rightPointer:
sum = nums[leftPointer] + nums[rightPointer]

if sum == target:
result.append([-1*target, nums[leftPointer], nums[rightPointer]])
while leftPointer < rightPointer and nums[leftPointer] == nums[leftPointer+1]:
leftPointer+=1
while leftPointer < rightPointer and nums[rightPointer] == nums[rightPointer-1]:
rightPointer-=1
leftPointer+=1
rightPointer-=1

elif sum < target:
leftPointer+=1
else:
rightPointer-=1

# return subResult

26 changes: 26 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Container With Most Water
#time complexity -> On
#space complexity -> O1
#logic, start from both ends, get the total volume, then whichever wall is smaller move the pointers inside from that wall as
# small wall is our limiting factor, idea is to find the biggest wall that can generate the most volume

from typing import List
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height)-1
result = 0
while left < right:
leftWall = height[left]
rightWall = height[right]
result = max(result,min(leftWall,rightWall)*(right-left))

if leftWall<rightWall:
left+=1
else:
right-=1
return result

sol = Solution()

print(sol.maxArea([3,2,3]))