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
37 changes: 37 additions & 0 deletions 3 Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#time complexity -0(n^2) and space complexity - o(1)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
result=[]
nums.sort()
n=len(nums)

for i in range (n-2):
if i!=0 and nums[i]==nums[i-1]:
continue
if nums[i]>0:
break
l= i+1
h=n-1
while l< h :
sum = nums[i]+nums[l]+nums[h]

if sum== 0:
result.append([nums[i],nums[l],nums[h]])

l= l+1
h= h-1

while (l<h and nums[l]==nums[l-1]):
l= l+1
while (l<h and nums[h]==nums[h+1]):
h = h-1
elif sum <0:
l= l+1
else:
h= h-1
return result





17 changes: 17 additions & 0 deletions Container with most water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#time complexity -0(n) and space complexity - o(1)
class Solution:
def maxArea(self, height: List[int]) -> int:
result =0

l= 0
h=len(height)-1

while l<h:
area = min(height[l], height[h])* (h-l)
result= max(result, area)

if height[l]<=height[h]:
l= l+1
else:
h= h-1
return result
21 changes: 21 additions & 0 deletions Sort Colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Time complexity - 0(n) & space complexity - o(1)
class Solution:
def sortColors(self, nums: List[int]) -> None:
if nums==None or len(nums)==0:
return
n= len(nums)
low=0 # storing 0
high= n-1 #storing 2
mid = 0 # storing 1

while mid<=high:
if nums[mid]==0:
nums[mid],nums[low]= nums[low], nums[mid]
low=low+1
mid=mid+1
elif nums[mid]==1:
mid = mid+1
else:
nums[mid],nums[high]=nums[high], nums[mid]
high = high-1