From a62ad94887346cb42b4f96003318df736ba02867 Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Sat, 15 Aug 2026 15:18:27 -0500 Subject: [PATCH] Completed Leetcode 11, 15 and 75 --- Problem 1-Leetcode75.py | 20 ++++++++++++++++++++ Problem 2-Leetcode15.py | 36 ++++++++++++++++++++++++++++++++++++ Problem 3- Leetcode11.py | 22 ++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 Problem 1-Leetcode75.py create mode 100644 Problem 2-Leetcode15.py create mode 100644 Problem 3- Leetcode11.py diff --git a/Problem 1-Leetcode75.py b/Problem 1-Leetcode75.py new file mode 100644 index 00000000..f14aa9e6 --- /dev/null +++ b/Problem 1-Leetcode75.py @@ -0,0 +1,20 @@ +#Time complexity: 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. + """ + l, m, h = 0, 0, len(nums) - 1 + while m <= h: + if nums[m] == 2: + nums[m], nums[h] = nums[h], nums[m] + h -= 1 + elif nums[m] == 0: + nums[m], nums[l] = nums[l], nums[m] + l += 1 + m += 1 + else: m += 1 + \ No newline at end of file diff --git a/Problem 2-Leetcode15.py b/Problem 2-Leetcode15.py new file mode 100644 index 00000000..c99bc1f3 --- /dev/null +++ b/Problem 2-Leetcode15.py @@ -0,0 +1,36 @@ +#Time complexity: O(n^2) +#Space complexity: O(1) + +class Solution(object): + def threeSum(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + if len(nums) < 3: + return [] + + n = len(nums) + nums.sort() + result = [] + + for i in range(n): + if i != 0 and nums[i] == nums[i - 1]: continue + if nums[i] > 0: break + l, r = i + 1, n - 1 + + while l < r: + sum = nums[i] + nums[l] + nums[r] + if sum == 0: + result.append([nums[i], nums[l], nums[r]]) + l += 1 + r -= 1 + while l < r and nums[l] == nums[l - 1] : l += 1 + while l < r and nums[r] == nums[r + 1] : r -= 1 + elif sum > 0: + r -= 1 + else: + l += 1 + + return result + \ No newline at end of file diff --git a/Problem 3- Leetcode11.py b/Problem 3- Leetcode11.py new file mode 100644 index 00000000..b43b0cdd --- /dev/null +++ b/Problem 3- Leetcode11.py @@ -0,0 +1,22 @@ +#Time complexity: O(n) +#Space complexity: O(1) + +class Solution(object): + def maxArea(self, height): + """ + :type height: List[int] + :rtype: int + """ + n = len(height) + l, r, m = 0, n - 1, 0 + + while l < r: + if height[l] < height[r]: + m = max(m, height[l] * (r - l)) + l += 1 + else: + m = max(m, height[r] * (r - l)) + r -= 1 + + return m + \ No newline at end of file