Skip to content

Commit 1d4ff54

Browse files
committed
3sum solution
1 parent 120df1f commit 1d4ff54

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

3sum/yuseok89.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# TC: O(N^2)
2+
# SC: O(N)
3+
class Solution:
4+
def threeSum(self, nums: list[int]) -> list[list[int]]:
5+
6+
cnt_dict = Counter(nums)
7+
nums_uniq = sorted(cnt_dict.keys())
8+
n = len(nums_uniq)
9+
10+
ret = []
11+
12+
for num in cnt_dict.keys():
13+
if num == 0:
14+
if cnt_dict[num] >= 3:
15+
ret.append([0, 0, 0])
16+
elif cnt_dict[num] >= 2 and num * 2 * -1 in cnt_dict:
17+
ret.append([num, num, num * 2 * -1])
18+
19+
for idx1, num1 in enumerate(nums_uniq):
20+
if num1 > 0:
21+
break
22+
23+
for idx2 in range(idx1 + 1, n):
24+
num2 = nums_uniq[idx2]
25+
num3 = (num1 + num2) * -1
26+
27+
if num2 >= num3:
28+
break
29+
elif num3 in cnt_dict:
30+
ret.append([num1, num2, num3])
31+
32+
return ret
33+

product-of-array-except-self/yuseok89.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// TC: O(N)
2-
// SC: O(N)
1+
# TC: O(N)
2+
# SC: O(N)
33
class Solution:
44
def productExceptSelf(self, nums: List[int]) -> List[int]:
55

0 commit comments

Comments
 (0)