Skip to content

Latest commit

ย 

History

History
111 lines (86 loc) ยท 3.95 KB

File metadata and controls

111 lines (86 loc) ยท 3.95 KB

๋ฌธ์ œ

์ˆซ์ž ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€๊ณ , ์ด ์ค‘ ๊ฐ’ 3๊ฐœ๋ฅผ ๊ณจ๋ผ 0 ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์˜ ์กฐํ•ฉ์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

์†”๋ฃจ์…˜

stack ์ด์šฉ

์˜ˆ์ œ์—์„œ [-1, 0, 1, 2, -1, -4] ์ด ๊ฐ’์—์„œ -1 -> 0, -1 -> 1 ... ์ด๋Ÿฐ ์‹์œผ๋กœ stack ์— ๋„ฃ๊ณ  ๋นผ๊ณ , backtracking ์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ (TLE) ์— ๊ฑธ๋ ค์„œ ํ๊ธฐ... ์ตœ๋Œ€ํ•œ ์ค„์—ฌ๋ณผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ๋„ ์ž˜ ์•ˆ๋˜์—ˆ์Œใ… ใ… 

์—ฌ๊ธฐ์„œ ๋” ์–ด๋–ป๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์„๊นŒ? ์ผ๋‹จ์€, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๊ฐ€ ํ‹€๋ฆฐ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ๋”ฐํํ‘...

from copy import deepcopy
from collections import defaultdict


class Solution:

    def dfs(self, nums, index, stack, result, cache):

        if index > len(nums) or len(stack) > 3:
            return

        s = sorted(stack)
        if len(stack) == 3:
            if (s[0], s[1]) in cache:
                return
            if (s[0], s[2]) in cache:
                return
            if (s[1], s[2]) in cache:
                return
            if sum(stack) == 0:
                cache[(s[0], s[1])] = True
                cache[(s[1], s[2])] = True
                cache[(s[0], s[2])] = True
                result.append(deepcopy(stack))
            return
        

        if len(stack) == 2:
            if (s[0], s[1]) in cache:
                return
            
        for j in range(index, len(nums)):
            stack.append(nums[j])
            self.dfs(nums, j + 1, stack, result, cache)
            stack.pop()

        if len(stack) == 2:
            cache[(s[0], s[1])] = True
        return

    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        stack = []
        cache = defaultdict(bool)
        nums = sorted(nums)
        for i in range(len(nums)):
            stack.append(nums[i])
            self.dfs(nums, i + 1, stack, result, cache)
            stack.pop()
        return result

์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ „์ฒด ๋ฐฐ์—ด N ๊ฐœ๋ผ๊ณ  ๋ดค์„ ๋•Œ, ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ N-1 ๊ฐœ์—์„œ 2๊ฐœ๋ฅผ ๊ณ ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— N * ((N-1) * (N-2) / 2) ์ธ ๊ฒƒ ๊ฐ™๋‹ค. (?) ๊ทธ๋Ÿฌ๋ฉด O(N^3) ์ด ๋งž๋‚˜...?

left, right ์ ‘๊ทผ

์ „์ฒด ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ํ›„์—, ํ˜„์žฌ ๊ฐ’ + ์™ผ์ชฝ ๊ฐ’ + ์˜ค๋ฅธ์ชฝ ๊ฐ’ ์„ ๋น„๊ต ํ•˜์—ฌ 0 ์ด๋ฉด ๊ทธ ๊ฐ’์„ ๊ฒฐ๊ณผ ๊ฐ’์— ์ถ”๊ฐ€, 0 ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์™ผ์ชฝ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ์ฆ๊ฐ€, 0 ๋ณด๋‹ค ํฌ๋ฉด ์˜ค๋ฅธ์ชฝ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ์†Œ ๋ฅผ ์ „์ฒด N ๋ฒˆ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

์ด๋ฏธ ์ •๋ ฌ์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „์— ๊ตฌํ–ˆ๋˜ ์™ผ์ชฝ ๊ฐ’์ด ์—ฐ์†ํ•ด์„œ ๊ฐ™์€ ์ˆ˜๋กœ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ (์˜ˆ๋ฅผ ๋“ค๋ฉด [-3, -1, -1, -1, 0, 4] ์™€ ๊ฐ™์ด ๋“ฑ์žฅ) ์—” -1 ์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™ผ์ชฝ ๊ฐ’์ด ๋‹ฌ๋ผ์งˆ ๋•Œ ๊นŒ์ง€ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ์ฆ๊ฐ€ ์‹œํ‚จ๋‹ค. ์˜ค๋ฅธ์ชฝ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์†Œ ์‹œํ‚จ๋‹ค.

์ด๋ ‡๊ฒŒ ์ „์ฒด N ๋ฒˆ๋งŒํผ ์ธ๋ฑ์Šค๋ฅผ ์ฆ๊ฐ€ ์‹œํ‚ค๋ฉด์„œ N-1 ๊ฐœ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ left, right ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(N^2) ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

from collections import defaultdict


class Solution:

    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        cache = defaultdict(bool)

        nums.sort()
        for i in range(len(nums) - 1):
            left, right = i + 1, len(nums) - 1
            while left < right:
                res = nums[i] + nums[left] + nums[right]
                if res < 0:
                    left += 1
                elif res > 0:
                    right -= 1
                else:
                    if (nums[i], nums[left], nums[right]) not in cache:
                        result.append([nums[i], nums[left], nums[right]])
                        cache[(nums[i], nums[left], nums[right])] = True
                    while left < right and nums[left] == nums[left + 1]:
                        left += 1
                    while left < right and nums[right] == nums[right - 1]:
                        right -= 1
                    left += 1
                    right -= 1

        return result