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
32 changes: 32 additions & 0 deletions container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Use two pointers at the start and end of the array to compute area formed by the lines.
# Move the pointer with the smaller height to potentially find a larger area, updating the maximum each step.






class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height) - 1
res = 0
while left < right:
if height[left] > height[right]:
curr_amt = (right - left) * height[right]
right -=1
else:
curr_amt = (right - left) * height[left]
left += 1
res = max(res,curr_amt)

return res




32 changes: 32 additions & 0 deletions sortColors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Use three pointers, l tracks position for next 0, h tracks position for next 2, m scans the array.


class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
l = 0
m = 0
h = len(nums) - 1

while m <= h:

if nums[m] == 2:
temp = nums[m]
nums[m] = nums[h]
nums[h] = temp
h -= 1
elif nums[m] == 0:
temp = nums[m]
nums[m] = nums[l]
nums[l] = temp
l += 1
m += 1
else:
m += 1

33 changes: 33 additions & 0 deletions threeSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Time Complexity : O(n²)
# Space Complexity : O(1) (excluding output)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Sort the array, fix one element, and use two pointers to find pairs whose sum with the fixed element equals zero.
# Move pointers based on the sum and skip duplicates to ensure only unique triplets are added.

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
res = []
nums.sort()
for i in range(len(nums)):
if i != 0 and nums[i] == nums[i - 1]:
continue
j = i + 1
k = len(nums) - 1
while j < k:
if nums[i] + nums[j] + nums[k] == 0:
res.append([nums[i],nums[j],nums[k]])
j += 1
k -= 1
while j < len(nums) - 1 and nums[j] == nums[j - 1]:
j += 1

while j < k and nums[k] == nums[k + 1]:
k -= 1
elif nums[i] + nums[j] + nums[k] > 0:
k -= 1
else:
j += 1
return res