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
51 changes: 51 additions & 0 deletions 3-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Run a loop through nums for each element with index i. Take l=i+1 and r=n-1.
Check the sum of the element with the element at r and l indecies. If the summation
is 0 increment the left pointer till a uniqure element is met and decrement the right
pointer. If the summation is less than 0, increse the left pointer till a unique
element is met. If the sum is greater than 0 decrese the right pointer

Time complexity: O(n2)
Space complexity: O(1)

class Solution(object):

def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
n=len(nums)
r=n-1
l=0
arr=[]
nums.sort()
for ind, i in enumerate(nums):
if ind-1>=0 and i==nums[ind-1]:
continue

l=ind+1
r=n-1
while l<r:
if l-1>ind and nums[l]==nums[l-1]:
l+=1
continue
s= i+ nums[r]+nums[l]
if s==0:
arr.append([i,nums[l],nums[r]])
l+=1
r-=1

elif s<0:
l+=1
else:
r-=1

return arr








39 changes: 39 additions & 0 deletions container-with-most-water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Take two pointers l=0 r=n-1. initialize max volume with 0
# while l<r loop through the array take the diffrence between the indices and
# multiply it to the shorteer height. Compare the vol with the previous max vol,
# if the current vol is greater, assign this val to max vol


class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
n=len(height)
r=n-1
l=0
mvol=0

while l<r:
v=(r-l)*min(height[l],height[r])
# print(v)
mvol=max(mvol,v)
if height[l]>height[r]:
r-=1
else:
l+=1
return mvol













26 changes: 26 additions & 0 deletions sort-colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Initialize an annray arr of length(nums) with 0 . Loop through nums, for each element
# x in nums, increment the acount of arr[x]. Iterate arr and replace nums[i] with the
# elemts in arr with their correspnding no. of times.

# Time compleity: O(n)
# Space complexity O(1)

class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
arr=[0, 0, 0]
for i in nums:
arr[i]=arr[i]+1
print(arr)
j=0
for ind, i in enumerate(arr):
n=j
while j<n+i:
nums[j]=ind
j+=1
return nums