"""
Q015
3Sum
Medium

20211020



This eventually worked, but very slow.
I used a hash table to check duplication.
Seems still really slow.

/Array
Two pointers/

Given an array nums of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which
gives the sum of zero.

Note:
The solution set must not contain duplicate triplets.

Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
"""
from typing import List

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

        # 2 pointer

        res = []
        nums.sort()

        for i in range(len(nums)):
            if i >= 1 and nums[i] == nums[i-1]:
                continue
            left, right = i + 1, len(nums) - 1
            while left < right:
                if nums[i] + nums[left] + nums[right] == 0:
                    res.append([nums[i], nums[left], nums[right]])
                    while left < len(nums) - 1 and nums[left+1] == nums[left]:
                        left += 1
                    left += 1
                    right -= 1
                    continue
                if nums[i] + nums[left] + nums[right] > 0:
                    right -= 1
                    continue
                if nums[i] + nums[left] + nums[right] < 0:
                    left += 1
                    continue

        return res



k = [1,-2,-5,-13,-10,-11,0,-12,-11,13,-4,9,8,10,-7,3,-9,-12,-7,8,-2,-12,1,-10,-15,-8,5,14,-7,-8,-8,9,-3,-6,3,-5,-1,-11,-10,3,-13,1,-10,3,-12,-10,-9,-13,-7,-1,10,6,-6,-12,12,-13,-13,-6,-14,-13,-7,-7,4,6,-6,-8,8,8,-4,13,-11,-1,-8,-14,9,-5,-9,7,-3,-1,14,14,13,-7,9,2,-5,12,11,-12,14,-11,-12,3,2,-2,3,-5,-9,14,-14,-13,-10,-7,-12,14,3,-6,-1,8,1,-2,-1,-1,6,-6]
sol = Solution()
print(sol.threeSum(k))






"""
Q015
3Sum
Medium

20211020



This eventually worked, but very slow.
I used a hash table to check duplication.
Seems still really slow.

/Array
Two pointers/

Given an array nums of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which
gives the sum of zero.

Note:
The solution set must not contain duplicate triplets.

Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
"""
from typing import List

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

        # 2 pointer

        res = []
        nums.sort()

        for i in range(len(nums)):
            if i >= 1 and nums[i] == nums[i-1]:
                continue
            left, right = i + 1, len(nums) - 1
            while left < right:
                if nums[i] + nums[left] + nums[right] == 0:
                    res.append([nums[i], nums[left], nums[right]])
                    while left < len(nums) - 1 and nums[left+1] == nums[left]:
                        left += 1
                    left += 1
                    right -= 1
                    continue
                if nums[i] + nums[left] + nums[right] > 0:
                    right -= 1
                    continue
                if nums[i] + nums[left] + nums[right] < 0:
                    left += 1
                    continue

        return res



k = [1,-2,-5,-13,-10,-11,0,-12,-11,13,-4,9,8,10,-7,3,-9,-12,-7,8,-2,-12,1,-10,-15,-8,5,14,-7,-8,-8,9,-3,-6,3,-5,-1,-11,-10,3,-13,1,-10,3,-12,-10,-9,-13,-7,-1,10,6,-6,-12,12,-13,-13,-6,-14,-13,-7,-7,4,6,-6,-8,8,8,-4,13,-11,-1,-8,-14,9,-5,-9,7,-3,-1,14,14,13,-7,9,2,-5,12,11,-12,14,-11,-12,3,2,-2,3,-5,-9,14,-14,-13,-10,-7,-12,14,3,-6,-1,8,1,-2,-1,-1,6,-6]
sol = Solution()
print(sol.threeSum(k))






Q015 3Sum Medium  20211020    This eventually worked, but very slow. I used a hash table to check duplication. Seems still really slow.  /Array Two pointers/  Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.  Note: The solution set must not contain duplicate triplets.  Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [   [-1, 0, 1],   [-1, -1, 2] ] # 2 pointer