๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

[leetcode] 15. 3Sum ํ’€์ด, ํ•ด์„ค (python)

ttoance 2023. 10. 28. 21:09

https://leetcode.com/problems/3sum/

 

3Sum - LeetCode

Can you solve this real interview question? 3Sum - Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain du

leetcode.com

 

1์ฐจ ํ’€์ด 

์ ‘๊ทผ๋ฒ•์„ ๋ชป์ฐพ์•„์„œ O(n*n)์œผ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ–ˆ๋‹ค. 

a,b๋ฅผ ์ฐพ์•„์„œ -(a + b)๊ฐ’์ด ๋‹ค๋ฅธ ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค. 

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        
        dset = set()
        for num in nums:
            if num not in dset:
                dset.add(num)
        
        
        result = set()
            
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                check = nums[i] + nums[j] 
          
                if -check in dset and nums.index(-check) not in [i, j]:
                    # ์ค‘๋ณต๊ฐ’ ์ฒดํฌ ์œ„ํ•ด ์ •๋ ฌํ•ด์„œ ํ‚ค๋ฅผ ์ƒ์„ฑ 
                    r = tuple(sorted([nums[i], nums[j], -check]))
                    result.add(r)
                
        
        return result

์‹œ๊ฐ„์ดˆ๊ณผ

 

2์ฐจ ํ’€์ด 

์ด ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž, ์ด๋ฏธ ํ™•์ธํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๋„˜์–ด๊ฐ€๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋‹ค. 

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        
        dset = defaultdict(list)
        for i, num in enumerate(nums):
            dset[num].append(i)
        
        result = set()
            
        # ์‹œ๊ฐ„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์ฒดํฌํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅ 
        checked = set()
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                check = nums[i] + nums[j] 

                if (nums[i], nums[j]) in checked:
                   continue
        
                if -check in dset:
                    # i,j ์กฐ๊ฑด ํ™•์ธ 
                    if dset[-check] == [i] or dset[-check] == [j] or dset[-check] == [i,j] or dset[-check] == [j, j]:
                        continue
                    
                    # ์ค‘๋ณต๊ฐ’ ์ฒดํฌ ์œ„ํ•ด ์ •๋ ฌํ•ด์„œ ํ‚ค๋ฅผ ์ƒ์„ฑ 
                    r = tuple(sorted([nums[i], nums[j], -check]))
                    result.add(r)

                # ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅํ•ด๋‘  
                checked.add((nums[i], nums[j]))
                
        
        return result

 

ํ•ด์„ค

https://www.youtube.com/watch?v=jzZsG8n2R9A

        res = []
        nums.sort()
        
        for i, a in enumerate(nums):
            if i > 0 and a == nums[i - 1]:
                continue 
                
            l, r = i + 1, len(nums) - 1
            while l < r:
                threeSum = a + nums[l] + nums[r]
                if threeSum > 0:
                    r -= 1
                elif threeSum < 0:
                    l += 1
                else:
                    res.append([a, nums[l], nums[r]])
                    l += 1
                    
                    while nums[l] == nums[l - 1] and l < r:
                        l += 1 
        
        return res

 

 

 

 

์ฐธ๊ณ ๋กœ, ํ•ด์„ค์€ ์—ฌ๊ธฐ ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•œ ์ ‘๊ทผ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. 

https://ddoance.tistory.com/149

 

[leetcode] 167. Two Sum II - Input Array Is Sorted ํ’€์ด, ํ•ด์„ค (python)

๋ฌธ์ œ ๋งํฌ https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Two Sum II - Input Array Is Sorted - LeetCode Can you solve this real interview question? Two Sum II - Input Array Is Sorted - Given a 1-indexed array of integers numbers that is

ddoance.tistory.com

 

๋ฐ˜์‘ํ˜•