diff --git a/3sum.py b/3sum.py new file mode 100644 index 00000000..e334f4a2 --- /dev/null +++ b/3sum.py @@ -0,0 +1,42 @@ +# Time Complexity : O(n^2) +# Space complexity :O(1) auxilary space. +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# Sort the array and iterate, keeping one element fixed, if fixed element is greter than 0 stop instantly. +# For each fixed element, use two pointer approach to find the other two elements, if sum is greater than 0 move right pointer, if sum is less than 0 move left pointer, if sum is 0 add the triplet to result and move both pointers. + +class Solution(object): + def threeSum(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + result = [] + nums.sort() + + for i,value in enumerate(nums): + # outside duplicate check + if i > 0 and value == nums[i - 1]: + continue + if value > 0: + break + l = i + 1 + r = len(nums) - 1 + while l < r: + res_sum = value + nums[l] + nums[r] + if res_sum > 0: + r -= 1 + elif res_sum < 0: + l +=1 + else: + result.append([value,nums[l],nums[r]]) + l +=1 + r -=1 + # to avoid duplicate values oterate until unique element + while nums[l] == nums[l - 1] and l < r: + l +=1 + while nums[r] == nums[r + 1] and l < r: + r -= 1 + return result \ No newline at end of file diff --git a/container_with_most_water.py b/container_with_most_water.py new file mode 100644 index 00000000..51c81e45 --- /dev/null +++ b/container_with_most_water.py @@ -0,0 +1,30 @@ +# Time Complexity : O(n) +# Space complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# start with two pointers at extreme ends of the array because the widest possible container is the starting point for the maximum area. +# calculates the current area based on the shorter of the two lengths and maintain max area. +# Move the poiinter that has the shorter height inward to maximize the area of container. + + +class Solution(object): + def maxArea(self, height): + """ + :type height: List[int] + :rtype: int + """ + #Method 2: efficient 2 pointer + result = 0 + l, r = 0, len(height)-1 + while l < r: + height_of_rect = min(height[l], height[r]) + area = (r - l) * height_of_rect + result = max(area, result) + + if height[l] < height[r]: + l += 1 + else: + r -= 1 + return result \ No newline at end of file diff --git a/sort_colors.py b/sort_colors.py new file mode 100644 index 00000000..add171d1 --- /dev/null +++ b/sort_colors.py @@ -0,0 +1,26 @@ +# Time Complexity : O(n) +# Space complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# using 3 pointers to divide the array into sections for 0s at the front, 2s at the back, and an unexplored zone in between +# curr acts as a scanner, f it sees a 0 swaps it to the low boundary, if it sees a 2 swaps it to the high boundary, if it sees a 1 just moves forward. + +class Solution(object): + def sortColors(self, nums): + """ + :type nums: List[int] + :rtype: None Do not return anything, modify nums in-place instead. + """ + low, curr, high = 0,0,len(nums)-1 + while curr <= high: + if nums[curr] == 0: + nums[low], nums[curr] = nums[curr], nums[low] + low += 1 + curr += 1 + elif nums[curr] == 1: + curr += 1 + else: + nums[high], nums[curr] = nums[curr], nums[high] + high -= 1 \ No newline at end of file