From 936c2b22767b8b2541af034a811f746d362361c4 Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 22 Feb 2026 14:04:15 -0800 Subject: [PATCH] Add implementation for leetcode problems 11, 15, 75 --- leetcode_11.py | 49 +++++++++++++++++++++++++++++++++++ leetcode_15.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ leetcode_75.py | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 leetcode_11.py create mode 100644 leetcode_15.py create mode 100644 leetcode_75.py diff --git a/leetcode_11.py b/leetcode_11.py new file mode 100644 index 00000000..497b36c9 --- /dev/null +++ b/leetcode_11.py @@ -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] 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 + + + + + + diff --git a/leetcode_75.py b/leetcode_75.py new file mode 100644 index 00000000..d7c57a34 --- /dev/null +++ b/leetcode_75.py @@ -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 + +