Skip to content

Commit 282670f

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents ee9872e + e0c4f4c commit 282670f

File tree

405 files changed

+11505
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

405 files changed

+11505
-132
lines changed

โ€Ž3sum/Geegong.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.*;
2+
3+
public class Geegong {
4+
5+
/**
6+
* Time complexity : O(n^2)
7+
* space complexity : O(n^2)
8+
* @param nums
9+
* @return
10+
*/
11+
public List<List<Integer>> threeSum(int[] nums) {
12+
13+
// ์ค‘๋ณต๋˜๋Š” ๊ฐ’์€ ์—†์–ด์•ผ ํ•˜๊ธฐ์— HashSet ์œผ๋กœ result
14+
HashSet<List<Integer>> result = new HashSet<>();
15+
16+
// Key : ๋ฐฐ์—ด ์›์†Œ , value : List<String> ์ธ๋ฑ์Šค๋“ค
17+
// elementMap ์€ two pointer ์˜ ๊ฐ’์„ ๋”ํ•œ ๊ฐ’์—์„œ 0์ด ๋˜๊ธฐ ์œ„ํ•œ ์š”์†Œ๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋  ๊ฒƒ์ž„
18+
// tc : O(n)
19+
Map<Integer, List<Integer>> elementMap = new HashMap<>();
20+
for (int index = 0; index<nums.length; index++) {
21+
int value = nums[index];
22+
if (elementMap.containsKey(value)) {
23+
List<Integer> indices = elementMap.get(value);
24+
indices.add(index);
25+
elementMap.put(value, indices);
26+
} else {
27+
List<Integer> newIndices = new ArrayList<>();
28+
newIndices.add(index);
29+
elementMap.put(value, newIndices);
30+
}
31+
}
32+
33+
// leftIndex : 0์—์„œ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” index
34+
// rightIndex : nums.length - 1์—์„œ๋ถ€ํ„ฐ ๊ฐ์†Œํ•˜๋Š” index
35+
// leftIndex > rightIndex ๋˜๋Š” ์ˆœ๊ฐ„๊นŒ์ง€๋งŒ for๋ฌธ์„ ๋Œ ๊ฒƒ์ด๋‹ค.
36+
// tc : O(N^2 / 2)
37+
for (int leftIndex=0; leftIndex < nums.length; leftIndex++) {
38+
for (int rightIndex=nums.length - 1; rightIndex >= 0; rightIndex--) {
39+
40+
if (leftIndex >= rightIndex) {
41+
break;
42+
}
43+
44+
45+
int leftValue = nums[leftIndex];
46+
int rightValue = nums[rightIndex];
47+
48+
int neededValueToZero = -leftValue - rightValue;
49+
if (elementMap.containsKey(neededValueToZero)) {
50+
// elementMap์˜ value ๊ฐ€ leftIndex, rightIndex ์€ ์•„๋‹Œ์ง€ ํ™•์ธ
51+
52+
List<Integer> indices = elementMap.get(neededValueToZero);
53+
// zero ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์„ธ๋ฒˆ์จฐ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
54+
int thirdIndex = findThirdIndexToBeZero(leftIndex, rightIndex, indices);
55+
if (-1 != thirdIndex) {
56+
List<Integer> newOne = new ArrayList<>();
57+
newOne.add(nums[leftIndex]);
58+
newOne.add(nums[rightIndex]);
59+
newOne.add(nums[thirdIndex]);
60+
result.add(newOne.stream().sorted().toList());
61+
}
62+
63+
}
64+
65+
}
66+
}
67+
68+
return result.stream().toList();
69+
70+
}
71+
72+
public int findThirdIndexToBeZero(int leftIndex, int rightIndex, List<Integer> indices) {
73+
for (int index : indices) {
74+
if (index != leftIndex && index != rightIndex) {
75+
return index;
76+
}
77+
}
78+
79+
return -1;
80+
}
81+
}
82+

โ€Ž3sum/bskkimm.py

Whitespace-only changes.

โ€Ž3sum/chrisjune.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
if len(nums) >= 3 and set(nums) == set([0]):
7+
return [[0, 0, 0]]
8+
triplets = set()
9+
for i in range(len(nums) - 2):
10+
seen = set()
11+
for j in range(i + 1, len(nums)):
12+
f = -(nums[i] + nums[j])
13+
if f in seen:
14+
triplet = [nums[i], nums[j], f]
15+
triplets.add(tuple(sorted(triplet)))
16+
seen.add(nums[j])
17+
return list(triplets)

โ€Ž3sum/delight010.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func threeSum(_ nums: [Int]) -> [[Int]] {
3+
let nums = nums.sorted()
4+
var answer: Set<[Int]> = []
5+
for (index, num) in nums.enumerated() {
6+
var leftIndex = index + 1
7+
var rightIndex = nums.endIndex - 1
8+
while leftIndex < rightIndex {
9+
let sum = num + nums[leftIndex] + nums[rightIndex]
10+
if sum == 0 {
11+
answer.insert([num, nums[leftIndex], nums[rightIndex]])
12+
leftIndex += 1
13+
rightIndex -= 1
14+
} else if sum < 0 {
15+
leftIndex += 1
16+
} else if sum > 0 {
17+
rightIndex -= 1
18+
}
19+
}
20+
}
21+
22+
return Array(answer)
23+
}
24+
}
25+

โ€Ž3sum/grapefruitgreentealoe.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//1.dfs๋กœ ํ’€๊ธฐ : ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚จ
2+
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number[][]}
7+
*/
8+
var threeSum = function(nums) {
9+
/*
10+
arr: ํ˜„์žฌ ๊ณ ์ • ๋ฐฐ์—ด์„ ๋œปํ•จ. dfs๊ฐ€ ๋Œ๋•Œ๋งˆ๋‹ค ๋ณ€๊ฒฝ๋จ.
11+
start: ๋๋‚œ index. arr์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๊ฒƒ์€ for๋ฌธ์•ˆ์—์„œ ๋ณ€๊ฒฝ๋  ์˜ˆ์ •.
12+
dep: dep์€ ํ˜„์žฌ dfs์˜ dep+1์„ ํ•ด์ฃผ๋ฉด ๋จ.
13+
*/
14+
const numsLength = nums.length
15+
if(numsLength ==3 && nums.reduce((a,b)=>a+b,0) == 0) return [nums]
16+
17+
const ret = []
18+
const seen = new Set();
19+
nums.sort((a, b) => a - b);
20+
21+
function dfs(arr,start,dep){
22+
if(dep == 3) {
23+
if(arr.length !==3) return
24+
const arrTotal = arr.reduce((a,b)=>a+b,0);
25+
if(arrTotal == 0){
26+
const string = [...arr].sort((a,b)=>a-b).join(',')
27+
if(!seen.has(string)){
28+
seen.add(string)
29+
ret.push(arr)
30+
}
31+
}
32+
return
33+
}
34+
//๋งŒ์•ฝ start๊ฐ€ 0์ด ๋˜๋ฉด, i๋ฅผ ๊ฐ์†Œ์‹œ์ผœ์•ผํ•˜๊ณ , ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค. ๊ธฐ์กด์˜ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค.
35+
36+
//๋์ ์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ์ง„ํ–‰ํ•ด์•ผํ•จ. ํ˜„์žฌ ๊ณ ์ • arr์— ๋์  ๊ทธ ์•ž์˜ idx๋ถ€ํ„ฐ ์ง„ํ–‰ํ•ด์„œ 0๊นŒ์ง€ ๊ฐ์†Œ์‹œํ‚ค๋ฉด์„œ dfs๋ฅผ ๋Œ๋ฆฌ๋ฉด ๋œ๋‹ค.
37+
//idx๊ฐ€ i-1๋ถ€ํ„ฐ์ด๋ฏ€๋กœ, i๋Š” ์ตœ์†Œ 1์ด์–ด์•ผ ๊ฐ€๋Šฅํ•˜๋‹ค.
38+
for(let idx = start; idx<numsLength; idx++){
39+
// currArr.push([...arr,nums[idx]])
40+
//ํ˜„์žฌ i์— ๋Œ€ํ•œ ํ˜„์žฌ currArr๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
41+
//๋งŒ๋“ค์–ด์ค€ ๊ฐ Arr์— ๋Œ€ํ•ด์„œ, ๋‹ค์‹œ dfs๋กœ ๋“ค์–ด๊ฐ€์„œ, ๊ฐ ๋ฐฐ์—ด์˜ ๋‚ด๋ถ€์˜ ํ•ฉ์ด 0์ธ์ง€ ํ™•์ธํ•˜๊ณ ,
42+
//ํ˜„์žฌ ๋ฐฐ์—ด์— ๋Œ€ํ•œ set์— ๋Œ€ํ•ด์„œ ์ค‘๋ณต์ด ๋˜๋Š” ๊ฒƒ์€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๋ฉด์„œ ๋„ฃ์–ด์ค€๋‹ค.
43+
dfs([...arr,nums[idx]],idx+1,dep+1)
44+
}}
45+
dfs([],0,0)
46+
//๋งˆ์ง€๋ง‰์— set์œผ๋กœ triples์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ค˜์•ผํ•œ๋‹ค.
47+
48+
return ret
49+
};
50+
51+
/*
52+
์‹œ๊ฐ„๋ณต์žก๋„:
53+
nums.sort((a, b) => a - b): ๋ฐฐ์—ด ์ •๋ ฌ์— O(NlogN)
54+
55+
N๊ฐœ ์ˆซ์ž ์ค‘ 3๊ฐœ ์„ ํƒํ•˜๋Š” ์กฐํ•ฉ ์ˆ˜ N(Nโˆ’1)(Nโˆ’2)/6
56+
์ •ํ™•ํžˆ ์•Œ ์ˆ˜๋Š” ์—†์ง€๋งŒ,
57+
58+
N^3 ์ด์ƒ์œผ๋กœ ๋ณด์ž„.
59+
60+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N^3) => seen ๋•Œ๋ฌธ.
61+
62+
*/
63+
64+
//2. ํˆฌํฌ์ธํ„ฐ๋กœ ํ’€๊ธฐ
65+
66+
/*
67+
์šฐ์„  ๋‚ด๊ฐ€ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ‹€๋ ธ๋‹ค. ์ˆซ์ž์˜ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๊ณ  ํ•˜๋”๋ผ๋„, ๊ฐ™์€ ์ˆซ์ž์˜ ์กฐํ•ฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ์•ˆ๋œ๋‹ค.
68+
๊ฐ’์ด ์›ํ•˜๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ์˜ฎ๊ธฐ๊ณ , ํฌ๋ฉด ์™ผ์ชฝ ๊ฐ’์„ ์˜ฎ๊ธฐ๋Š” ํˆฌํฌ์ธํ„ฐ ์ „๋žต์„ ์ทจํ•ด๋ณด์•˜๋‹ค.
69+
70+
*/
71+
72+
/**
73+
* @param {number[]} nums
74+
* @return {number[][]}
75+
*/
76+
var threeSum = function(nums) {
77+
const triplets = []
78+
nums.sort((a,b)=>a-b)
79+
for(let i =0;i<nums.length -2 ;i++){
80+
if (i > 0 && nums[i] === nums[i - 1]) continue;
81+
let low = i+1
82+
,high = nums.length -1
83+
84+
while(low < high){
85+
const three_sum = nums[i] + nums[low] + nums[high]
86+
if(three_sum<0){
87+
low +=1
88+
}
89+
else if(three_sum >0){
90+
high -=1
91+
}
92+
else{
93+
triplets.push([nums[i],nums[low],nums[high]])
94+
while (low < high && nums[low] === nums[low + 1]) low++;
95+
while (low < high && nums[high] === nums[high - 1]) high--;
96+
low = low+1
97+
high = high -1
98+
}
99+
}
100+
}
101+
return triplets
102+
};

โ€Ž3sum/hi-rachel.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
1-
# O(n^2) time, O(n) space
1+
"""
2+
https://leetcode.com/problems/3sum/
3+
4+
๋ฌธ์ œ ์„ค๋ช…:
5+
์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,
6+
์„ธ ์ˆ˜์˜ ํ•ฉ์ด 0์ด ๋˜๋Š” ๋ชจ๋“  ์œ ๋‹ˆํฌํ•œ triplet์„ ์ฐพ๋Š” ๋ฌธ์ œ.
7+
8+
์กฐ๊ฑด:
9+
- ์ค‘๋ณต ์กฐํ•ฉ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ.
10+
- ์˜ˆ: [-1, 0, 1]๊ณผ [0, -1, 1]์€ ๊ฐ™์€ ์กฐํ•ฉ์ด๋ฏ€๋กœ ํ•˜๋‚˜๋งŒ ํฌํ•จ.
11+
12+
ํ’€์ด ์ „๋žต:
13+
1. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค.
14+
2. ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋ฅผ ๊ณ ์ •ํ•œ ๋’ค, ๋‚˜๋จธ์ง€ ๋‘ ์ˆ˜๋ฅผ ํˆฌํฌ์ธํ„ฐ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
15+
- ํ•ฉ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด left๋ฅผ ์ฆ๊ฐ€
16+
- ํ•ฉ์ด 0๋ณด๋‹ค ํฌ๋ฉด right๋ฅผ ๊ฐ์†Œ
17+
- ํ•ฉ์ด 0์ด๋ฉด ์ •๋‹ต์— ์ถ”๊ฐ€, ์ค‘๋ณต ๋ฐฉ์ง€ ์ฒ˜๋ฆฌ๋„ ํ•จ๊ป˜ ์ˆ˜ํ–‰
18+
19+
TC: O(n^2), ์ •๋ ฌ O(n log n) + ํˆฌํฌ์ธํ„ฐ O(n^2)
20+
SC: O(1), ์ถœ๋ ฅ ๊ณต๊ฐ„ result ์ œ์™ธ
21+
"""
22+
23+
from typing import List
24+
25+
26+
class Solution:
27+
def threeSum(self, nums: List[int]) -> List[List[int]]:
28+
nums.sort()
29+
result = []
30+
31+
for i in range(len(nums) - 2):
32+
if i > 0 and nums[i] == nums[i - 1]:
33+
continue # ์ค‘๋ณต ์ œ๊ฑฐ
34+
35+
left, right = i + 1, len(nums) - 1
36+
37+
while left < right:
38+
total = nums[i] + nums[left] + nums[right]
39+
# ์กฐ๊ฑด ๋งŒ์กฑ์‹œ ์ •๋‹ต์— ์ถ”๊ฐ€
40+
if total == 0:
41+
result.append([nums[i], nums[left], nums[right]])
42+
# ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ left๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด, ์ค‘๋ณต ๊ฑด๋„ˆ๋œ€
43+
while left < right and nums[left] == nums[left + 1]:
44+
left += 1
45+
# ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ right๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด, ์ค‘๋ณต ๊ฑด๋„ˆ๋œ€
46+
while left < right and nums[right] == nums[right - 1]:
47+
right -= 1
48+
left += 1
49+
right -= 1
50+
# ์ดํ•ฉ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด, ๋” ํฐ ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ˆ๊นŒ left๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
51+
elif total < 0:
52+
left += 1
53+
# ์ดํ•ฉ์ด 0๋ณด๋‹ค ํฌ๋ฉด, ๋” ์ž‘์€ ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ˆ๊นŒ right๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
54+
else:
55+
right -= 1
256

57+
return result
58+
59+
60+
61+
# O(n^2) time, O(n) space
362
class Solution:
463
def threeSum(self, nums: List[int]) -> List[List[int]]:
564
triplets = set()

โ€Ž3sum/hj4645.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort() # ๋ฐฐ์—ด ์ •๋ ฌํ•ด์„œ ์ค‘๋ณต ์ฒ˜๋ฆฌ์™€ ํˆฌ ํฌ์ธํ„ฐ์— ์œ ๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ฆ
4+
n = len(nums)
5+
answer = []
6+
7+
for i in range(n - 2):
8+
# i๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด์„œ ์ด์ „ ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฉด ์ค‘๋ณต ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ๊ฑด๋„ˆ๋œ€
9+
if i > 0 and nums[i] == nums[i - 1]:
10+
continue
11+
12+
left, right = i + 1, n - 1
13+
14+
while left < right:
15+
total = nums[i] + nums[left] + nums[right]
16+
17+
if total == 0:
18+
answer.append([nums[i], nums[left], nums[right]])
19+
20+
# left์™€ right๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์ด ์ค‘๋ณต์ด๋ฉด ๋„˜์–ด๊ฐ
21+
while left < right and nums[left] == nums[left + 1]:
22+
left += 1
23+
while left < right and nums[right] == nums[right - 1]:
24+
right -= 1
25+
26+
left += 1
27+
right -= 1
28+
elif total < 0:
29+
left += 1
30+
else:
31+
right -= 1
32+
33+
return answer

โ€Ž3sum/hoyeongkwak.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Time Complexity : O(n^2)
3+
Space Complexity : O(1)
4+
*/
5+
class Solution {
6+
public List<List<Integer>> threeSum(int[] nums) {
7+
List<List<Integer>> result = new ArrayList<>();
8+
Arrays.sort(nums);
9+
10+
for (int i = 0; i < nums.length - 2; i++) {
11+
if (i > 0 && nums[i] == nums[i - 1]) continue;
12+
int low = i + 1;
13+
int high = nums.length - 1;
14+
while (low < high) {
15+
int threeSum = nums[i] + nums[low] + nums[high];
16+
if (threeSum < 0) {
17+
low = low + 1;
18+
} else if (threeSum > 0) {
19+
high = high - 1;
20+
} else {
21+
result.add(Arrays.asList(nums[i], nums[low], nums[high]));
22+
while (low < high && nums[low] == nums[low + 1]) low++;
23+
while (low < high && nums[high] == nums[high - 1]) high--;
24+
low = low + 1;
25+
high = high - 1;
26+
}
27+
}
28+
}
29+
30+
return result;
31+
}
32+
}

โ€Ž3sum/hu6r1s.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
"""
3+
1. 3 way for๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ 0์ธ ํ•ฉ์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•
4+
- O(n^3)์œผ๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ
5+
2. ํˆฌํฌ์ธํ„ฐ
6+
- ์ •๋ ฌ ํ›„ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต ์ œ๊ฑฐ์™€ ์ตœ์ ํ™”๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰
7+
- O(n^2)
8+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” ๋‘˜๋‹ค O(1)
9+
"""
10+
def threeSum(self, nums: List[int]) -> List[List[int]]:
11+
result = set()
12+
nums.sort()
13+
14+
for i in range(len(nums)):
15+
left, right = i + 1, len(nums) - 1
16+
while left < right:
17+
total = nums[i] + nums[left] + nums[right]
18+
19+
if total == 0:
20+
result.add((nums[i], nums[left], nums[right]))
21+
left += 1
22+
right -= 1
23+
elif total < 0:
24+
left += 1
25+
else:
26+
right -= 1
27+
28+
return list(result)

0 commit comments

Comments
ย (0)