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
49 changes: 49 additions & 0 deletions leetcode_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 14:03:32 2026

@author: rishigoswamy

LeetCode 11: Container With Most Water
Link: https://leetcode.com/problems/container-with-most-water/

Approach:
Two Pointers.

Start with the widest container (left=0, right=n-1).
Area = min(height[left], height[right]) * (right - left)

To potentially find a larger area, we must increase the limiting height.
Therefore:
- If height[left] < height[right], move left pointer rightward.
- Else, move right pointer leftward.

This works because moving the taller pointer cannot increase the min height
(the shorter one is still the bottleneck), but it will definitely decrease width.

// Time Complexity : O(n)
Each pointer moves at most n times.
// Space Complexity : O(1)
Constant extra space.
"""

from typing import List

class Solution:
def maxArea(self, height: List[int]) -> int:
maxarea = float('-inf')

l = 0
r = len(height)-1

while l<=r:
maxarea = max(min(height[l], height[r]) * (r-l), maxarea)

if height[l]<height[r]:
l+=1
else:
r-=1

return maxarea

69 changes: 69 additions & 0 deletions leetcode_15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 14:01:34 2026

@author: rishigoswamy

LeetCode 15: 3Sum
Link: https://leetcode.com/problems/3sum/

Approach:
Sort + Two Pointers.

Steps:
1. Sort the array.
2. Fix one number nums[i].
3. Use two pointers (left, right) to find pairs such that:
nums[i] + nums[left] + nums[right] = 0
4. Skip duplicates carefully for:
- the fixed index i
- left pointer after finding a valid triplet

Important:
Duplicate handling must be done correctly,
otherwise results may repeat or valid results may be skipped.

// Time Complexity : O(n^2)
Sorting: O(n log n)
Outer loop + two-pointer scan: O(n^2)
// Space Complexity : O(1)
Ignoring output list.
"""

from typing import List

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()

resList = []

print(nums)
i = 0
while i< len(nums):
target = 0 - nums[i]
left = i+1
right = len(nums) - 1
while (left < right):
if ((nums[left] + nums[right]) == target):
resList.append ([nums[i], nums[left], nums[right]])
left +=1
right-=1
while i+1 < len(nums) and nums[i+1] == nums[i]:
i+=1
while left < len(nums)-1 and nums[left-1] == nums[left]:
left+=1
elif((nums[left] + nums[right]) > target):
right-=1
elif((nums[left] + nums[right]) < target):
left+=1
i+=1

return resList






57 changes: 57 additions & 0 deletions leetcode_75.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 13:54:45 2026

@author: rishigoswamy

LeetCode 75: Sort Colors
Link: https://leetcode.com/problems/sort-colors/

Approach:
Dutch National Flag Algorithm (Three Pointers).

Maintain three regions:
[0 ... p0-1] -> all 0s
[p0 ... curr-1] -> all 1s
[curr ... p2] -> unknown
[p2+1 ... end] -> all 2s

Rules:
If nums[curr] == 0:
swap with p0, move both p0 and curr forward
If nums[curr] == 2:
swap with p2, move p2 backward
(do NOT move curr because swapped element needs evaluation)
If nums[curr] == 1:
just move curr forward

// Time Complexity : O(n)
Each element is processed at most once.
// Space Complexity : O(1)
In-place sorting.
"""

from typing import List

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

while curr <= p2:
if nums[curr] == 0:
nums[curr], nums[p0] = nums[p0], nums[curr]
p0+=1
curr+=1
elif nums[curr] == 2:
nums[curr], nums[p2] = nums[p2], nums[curr]
p2-=1
else:
curr+=1