From 409d31127eb7646746dc816c53969a06733bfc83 Mon Sep 17 00:00:00 2001 From: Tu-Code Date: Tue, 11 Oct 2022 00:56:35 +0100 Subject: [PATCH 1/2] 3 sum Leetcode solution using Python - by Sarah --- Python/3sum.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Python/3sum.py diff --git a/Python/3sum.py b/Python/3sum.py new file mode 100644 index 0000000..6343402 --- /dev/null +++ b/Python/3sum.py @@ -0,0 +1,28 @@ +# Leetcode Problem Link: https://leetcode.com/problems/3sum/ +# Name: Sarah Solarin +# Github ID: Tu-code +# File extension: .py + +class Solution: + # def threeSum(self, nums: List[int]) -> List[List[int]]: + def threeSum(self, nums): + ans = [] + nums.sort() + + for i, n in enumerate(nums): + if i > 0 and n == nums[i - 1]: + continue + + l, r = i + 1, len(nums) - 1 + while l < r: + threeSum = n + nums[l] + nums[r] + if threeSum > 0: + r -= 1 + elif threeSum < 0: + l += 1 + else: + ans.append([n, nums[l], nums[r]]) + l += 1 + while nums[l] == nums[l - 1] and l < r: + l += 1 + return ans From 746cdf0b182b8ff075126a087457dff70ab85607 Mon Sep 17 00:00:00 2001 From: Tu-Code Date: Tue, 11 Oct 2022 01:36:24 +0100 Subject: [PATCH 2/2] Container with most water in Python --- Python/ContainerWithMostWater.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Python/ContainerWithMostWater.py diff --git a/Python/ContainerWithMostWater.py b/Python/ContainerWithMostWater.py new file mode 100644 index 0000000..8fdf0b2 --- /dev/null +++ b/Python/ContainerWithMostWater.py @@ -0,0 +1,18 @@ +# Leetcode Problem Link: https://leetcode.com/problems/container-with-most-water/ +# Name: Sarah Solarin +# Github ID: Tu-code +# File extension: .py + +class Solution: + # def maxArea(self, height: List[int]) -> int: + def maxArea(self, height): + l, r = 0, len(height) - 1 + res = 0 + + while l < r: + res = max(res, min(height[l], height[r]) * (r - l)) + if height[l] < height[r]: + l += 1 + elif height[r] <= height[l]: + r -= 1 + return res \ No newline at end of file