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 3sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# // Time Complexity : O(nlogn + n^2) => O(n^2)
# // Space Complexity : sorting takes O(n)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : I was trying to do twosum with just rest of the array and taregt as 0-nums[i].
# It was hard to get to the point where I should try and form the result set of 3 numbers while handling 2 sum, instead of just getting the 2 nums other than i and then figuring out to remove duplicates.

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
resultset=[]
nums.sort()
i=0
while i<len(nums):
if nums[i]>0: break
if i!=0 and nums[i-1]==nums[i]:
i+=1
else:
start=i+1
end=len(nums)-1
while start< end:
s=nums[i]+nums[start]+nums[end]
if s==0:
resultset.append([nums[i],nums[start],nums[end]])
start+=1
end-=1
while start<end and nums[start]==nums[start-1]:
start+=1
elif s <0:
start+=1
else:
end-=1
i+=1

return resultset
27 changes: 27 additions & 0 deletions maxwater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# // Time Complexity : O(n)
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : No

class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""

res=0
start=0
end=len(height)-1

while start<end:
width=end-start
ht = min(height[start],height[end])
res = max(res, width*ht)
if height[start]<height[end]:
start+=1
else:
end-=1

return res

26 changes: 26 additions & 0 deletions sortcolors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# // Time Complexity : O(n)
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : idea to inplace sorting did not come to me until class.

class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
low=0
mid=0
high=len(nums)-1

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