Skip to content

Commit a036575

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents d8a0117 + 1d6ee79 commit a036575

File tree

984 files changed

+34714
-595
lines changed

Some content is hidden

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

984 files changed

+34714
-595
lines changed

3sum/HoonDongKang.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* [Problem]: [15] 3Sum
3+
* (https://leetcode.com/problems/3sum/description/)
4+
*/
5+
6+
function threeSum(nums: number[]): number[][] {
7+
//시간 복잡도: O(n^2)
8+
//공간 복잡도: O(1)
9+
function pointerFunc(nums: number[]): number[][] {
10+
const result: number[][] = [];
11+
nums.sort((a, b) => a - b);
12+
13+
for (let i = 0; i < nums.length; i++) {
14+
if (i > 0 && nums[i] === nums[i - 1]) continue;
15+
let left = i + 1;
16+
let right = nums.length - 1;
17+
18+
while (left < right) {
19+
const sum = nums[i] + nums[left] + nums[right];
20+
if (sum === 0) {
21+
result.push([nums[i], nums[left], nums[right]]);
22+
23+
while (left < right && nums[left] === nums[left + 1]) left++;
24+
while (left < right && nums[right] === nums[right - 1]) right--;
25+
26+
left++;
27+
right--;
28+
} else if (sum < 0) {
29+
left++;
30+
} else {
31+
right--;
32+
}
33+
}
34+
}
35+
return result;
36+
}
37+
38+
return pointerFunc(nums);
39+
}

3sum/JEONGBEOMKO.java

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

3sum/Jeehay28.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Approach 3
2+
// 🗓️ 2025-04-12
3+
// ⏳ Time Complexity: O(n log n) + O(n^2) = O(n^2)
4+
// 💾 Space Complexity: O(1) (excluding output)
5+
6+
function threeSum(nums: number[]): number[][] {
7+
8+
nums.sort((a, b) => a - b); // O(n log n)
9+
let result: number[][] = [];
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
13+
if (i > 0 && nums[i - 1] == nums[i]) continue; // skip duplicate i
14+
15+
let low = i + 1, high = nums.length - 1;
16+
// two-pointer scan (low and high) -> takes up to O(n) time per iteration
17+
while (low < high) {
18+
const threeSum = nums[i] + nums[low] + nums[high];
19+
if (threeSum < 0) {
20+
low += 1;
21+
} else if (threeSum > 0) {
22+
high -= 1;
23+
} else {
24+
result.push([nums[i], nums[low], nums[high]]);
25+
26+
while (low < high && nums[low] === nums[low + 1]) low += 1 // skip duplicate low
27+
while (low < high && nums[high] === nums[high - 1]) high -= 1 // skip duplicate high
28+
29+
low += 1;
30+
high -= 1;
31+
32+
}
33+
}
34+
}
35+
return result
36+
}
37+
38+
39+
// Approach 2
40+
// 🗓️ 2025-04-11
41+
// ❌ Time Limit Exceeded 313 / 314 testcases passed
42+
// ⏳ Time Complexity: O(n^2)
43+
// 💾 Space Complexity: O(n^2)
44+
45+
// function threeSum(nums: number[]): number[][] {
46+
// const result: number[][] = [];
47+
// const triplets = new Set<string>();
48+
49+
// for (let i = 0; i < nums.length - 2; i++) {
50+
// const seen = new Set<number>();
51+
// for (let j = i + 1; j < nums.length; j++) {
52+
// const twoSum = nums[i] + nums[j];
53+
// if (seen.has(-twoSum)) {
54+
// const triplet = [nums[i], nums[j], -twoSum].sort((a, b) => a - b); // O(log 3) = O(1)
55+
// const key = triplet.join(",");
56+
// if (!triplets.has(key)) {
57+
// triplets.add(key);
58+
// result.push(triplet);
59+
// }
60+
// }
61+
// seen.add(nums[j]);
62+
// }
63+
// }
64+
// return result;
65+
// }
66+
67+
68+
// Approach 1
69+
// 🗓️ 2025-04-11
70+
// ❌ Time Limit Exceeded!
71+
// ⏳ Time Complexity: O(n^3)
72+
// 💾 Space Complexity: O(n^2)
73+
74+
// function threeSum(nums: number[]): number[][] {
75+
76+
// let triplets = new Set<string>();
77+
// let result: number[][] = [];
78+
79+
// // const set = new Set();
80+
// // set.add([1, 2, 3]);
81+
// // set.add([1, 2, 3]);
82+
// // console.log(set); // contains BOTH arrays
83+
// // Set(2) { [ 1, 2, 3 ], [ 1, 2, 3 ] }
84+
85+
// for (let i = 0; i < nums.length - 2; i++) {
86+
// for (let j = i + 1; j < nums.length - 1; j++) {
87+
// for (let k = j + 1; k < nums.length; k++) {
88+
// if (nums[i] + nums[j] + nums[k] === 0) {
89+
// const triplet = [nums[i], nums[j], nums[k]].sort((a, b) => a - b);
90+
// const key = triplet.join(",");
91+
// if (!triplets.has(key)) {
92+
// triplets.add(key);
93+
// result.push(triplet)
94+
// }
95+
// }
96+
// }
97+
// }
98+
// }
99+
100+
// return result;
101+
// };

3sum/Sung-Heon.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+
result = []
5+
n = len(nums)
6+
7+
for i in range(n - 2):
8+
if i > 0 and nums[i] == nums[i - 1]:
9+
continue
10+
11+
left = i + 1
12+
right = n - 1
13+
14+
while left < right:
15+
current_sum = nums[i] + nums[left] + nums[right]
16+
17+
if current_sum == 0:
18+
result.append([nums[i], nums[left], nums[right]])
19+
20+
while left < right and nums[left] == nums[left + 1]:
21+
left += 1
22+
while left < right and nums[right] == nums[right - 1]:
23+
right -= 1
24+
25+
left += 1
26+
right -= 1
27+
28+
elif current_sum < 0:
29+
left += 1
30+
else:
31+
right -= 1
32+
33+
return result

3sum/Yn3-3xh.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
[문제풀이]
3+
- nums 배열 안의 3가지 수를 더했을 때 0이어야 한다.
4+
- 더해진 수들의 index는 모두 달라야 한다.
5+
- DFS (X)
6+
StackOverflowError
7+
- 투 포인터 (O)
8+
time: O(N^2), space: O(N^2)
9+
10+
[회고]
11+
해설을 보고 이해는 했는데 다시 풀 수 있을까?
12+
아직 스킬이 부족한 것 같다..
13+
14+
*/
15+
class Solution {
16+
public List<List<Integer>> threeSum(int[] nums) {
17+
Set<List<Integer>> answers = new HashSet<>();
18+
Arrays.sort(nums);
19+
20+
for (int start = 0; start < nums.length; start++) {
21+
int mid = start + 1;
22+
int end = nums.length - 1;
23+
24+
while (mid < end) {
25+
int sum = nums[start] + nums[mid] + nums[end];
26+
if (sum == 0) {
27+
List<Integer> answer = List.of(nums[start], nums[mid], nums[end]);
28+
answers.add(answer);
29+
end--;
30+
} else if (sum < 0) {
31+
mid++;
32+
} else if (sum > 0) {
33+
end--;
34+
}
35+
}
36+
}
37+
return new ArrayList<>(answers);
38+
}
39+
}

3sum/YoungSeok-Choi.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
// NOTE: 실행시간 1.5초..
9+
// 시간 복잡도: O(n^2)
10+
class Solution {
11+
public List<List<Integer>> threeSum(int[] nums) {
12+
13+
Arrays.sort(nums);
14+
Set<List<Integer>> result = new HashSet<>();
15+
16+
for(int i = 0; i < nums.length - 2; i++) {
17+
int startIdx = i + 1;
18+
int endIdx = nums.length - 1;
19+
while(startIdx < endIdx) {
20+
int sum = nums[i] + nums[startIdx] + nums[endIdx];
21+
if(sum == 0) {
22+
result.add(Arrays.asList(nums[i], nums[startIdx], nums[endIdx]));
23+
}
24+
25+
if(sum > 0) {
26+
endIdx--;
27+
} else {
28+
startIdx++;
29+
}
30+
}
31+
}
32+
33+
return new ArrayList<>(result);
34+
}
35+
}
36+
37+
38+
39+
// 309 / 314 testcases passed Time Limit Exceeded
40+
// NOTE: 시간복잡도 O(n^3)
41+
class WrongSolution {
42+
public List<List<Integer>> threeSum(int[] nums) {
43+
Set<List<Integer>> res = new HashSet<>();
44+
45+
List<Integer> temp = new ArrayList<>();
46+
47+
temp.remove(3);
48+
49+
for(int i = 0; i < nums.length; i++) {
50+
for(int j = 0; j < i; j++) {
51+
for(int k = 0; k < j; k++) {
52+
53+
if((nums[i] + nums[j] + nums[k]) == 0) {
54+
List<Integer> solution = Arrays.asList(nums[i], nums[j], nums[k]);
55+
Collections.sort(solution);
56+
res.add(solution);
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
return new ArrayList<>(res);
64+
}
65+
}

3sum/ayleeee.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 투포인터를 활용해보기
2+
3+
def threeSum(self, nums: List[int]) -> List[List[int]]:
4+
n = len(nums)
5+
nums.sort()
6+
result = set()
7+
for i in range(n):
8+
l,r = i+1,n-1
9+
while l<r:
10+
res = nums[i] + nums[l] + nums[r]
11+
if res < 0:
12+
l += 1
13+
elif res > 0:
14+
r -= 1
15+
else:
16+
result.add((nums[i], nums[l], nums[r]))
17+
l += 1
18+
return list(result)
19+

0 commit comments

Comments
 (0)