From 116e233e661fe0b803208490ac342bd0b07effd0 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH] feat: 15. 3Sum - Time Limit --- 3sum/HodaeSsi.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 3sum/HodaeSsi.py diff --git a/3sum/HodaeSsi.py b/3sum/HodaeSsi.py new file mode 100644 index 000000000..5bc0015c6 --- /dev/null +++ b/3sum/HodaeSsi.py @@ -0,0 +1,22 @@ +# 시간복잡도 O(n^2), 공간복잡도 O(n^2) +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + # key: 값, value: list((i, j)) + dic = {} + answer = set() + + # 이중 for문으로 모든 경우의 수를 구합니다. + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i] + nums[j] in dic: + dic[nums[i] + nums[j]].append((i, j)) + else: + dic[nums[i] + nums[j]] = [(i, j)] + + for k in range(len(nums)): + for i, j in dic.get(-nums[k], []): + if i != k and j != k: + answer.add(tuple(sorted([nums[i], nums[j], nums[k]]))) + + return list(answer) +