Skip to content

Commit 4edb975

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 7775163 + 85a306c commit 4edb975

119 files changed

Lines changed: 3610 additions & 44 deletions

File tree

Some content is hidden

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

3sum/Cyjin-jani.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ! 풀다가 막혀서 AI의 도움(힌트)을 받아 완성한 코드입니다..
2+
3+
// tc: o(n^2)
4+
// sc: o(n)
5+
const threeSum = function (nums) {
6+
const answer = [];
7+
8+
// 먼저 오름차순 정렬하기
9+
const sortedArr = nums.sort((a, b) => a - b);
10+
11+
for (let i = 0; i < sortedArr.length; i++) {
12+
if (i > 0 && sortedArr[i] === sortedArr[i - 1]) continue;
13+
14+
let left = i + 1;
15+
let right = sortedArr.length - 1;
16+
17+
while (left < right) {
18+
const sum = sortedArr[i] + sortedArr[left] + sortedArr[right];
19+
20+
if (sum === 0) {
21+
answer.push([sortedArr[i], sortedArr[left], sortedArr[right]]);
22+
23+
// 중복 처리
24+
// left가 가리키는 숫자가 이전 숫자와 똑같다면 계속 패스
25+
while (left < right && sortedArr[left] === sortedArr[left + 1]) {
26+
left++;
27+
}
28+
// right가 가리키는 숫자가 이전 숫자와 똑같다면 계속 패스
29+
while (left < right && sortedArr[right] === sortedArr[right - 1]) {
30+
right--;
31+
}
32+
// 똑같은 숫자들을 다 건너뛰었으니, 새로운 숫자로 넘어감
33+
left++;
34+
right--;
35+
} else if (sum < 0) {
36+
// 0보다 작으면 left를 키움 (오름차순 정렬이기 때문)
37+
left++;
38+
} else {
39+
right--;
40+
}
41+
}
42+
}
43+
44+
return answer;
45+
};

3sum/YOOHYOJEONG.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://leetcode.com/problems/3sum
2+
3+
class Solution(object):
4+
def threeSum(self, nums):
5+
nums.sort()
6+
result = []
7+
8+
for i in range(len(nums) - 2):
9+
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
left = i + 1
14+
right = len(nums) - 1
15+
16+
while left < right:
17+
total = nums[i] + nums[left] + nums[right]
18+
19+
if total < 0:
20+
left += 1
21+
22+
elif total > 0:
23+
right -= 1
24+
25+
else:
26+
result.append([nums[i], nums[left], nums[right]])
27+
28+
while left < right and nums[left] == nums[left + 1]:
29+
left += 1
30+
31+
while left < right and nums[right] == nums[right - 1]:
32+
right -= 1
33+
34+
left += 1
35+
right -= 1
36+
37+
return result

3sum/Yu-Won.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/3sum/description/
3+
*
4+
* 요구사항:
5+
* nums: number[]가 주어질 때 3개의 합이 0이되는 배열을 리턴한다.
6+
*
7+
* * */
8+
9+
const threeSum = (nums) => {
10+
let result = [];
11+
12+
nums.sort((a,b) => a-b);
13+
14+
for(let i = 0; i <nums.length-2; i++) {
15+
if(i > 0 && nums[i] === nums[i-1]) continue;
16+
17+
if(nums[i] > 0) break;
18+
19+
let left= i+1;
20+
let right = nums.length -1;
21+
22+
while(left < right) {
23+
let sum = nums[i] + nums[left] + nums[right];
24+
25+
if(sum === 0) {
26+
result.push([nums[i], nums[left], nums[right]]);
27+
28+
while(left < right && nums[left] === nums[left+1]) left++;
29+
while(left < right && nums[right] === nums[right-1]) right++;
30+
left++;
31+
right--;
32+
} else if (sum <0) {
33+
left++;
34+
} else {
35+
right--;
36+
}
37+
}
38+
}
39+
return result;
40+
}

3sum/dohyeon2.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Arrays;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
class Solution {
6+
// TC: O(n^2)
7+
// SC: O(n^2)
8+
public List<List<Integer>> threeSum(int[] nums) {
9+
List<List<Integer>> answer = new ArrayList<List<Integer>>();
10+
11+
// Sort the array to use two-pointer technique.
12+
Arrays.sort(nums);
13+
14+
// Exclude the last two elements from the loop
15+
// since the two pointers are involved in the iteration.
16+
for (int i = 0; i < nums.length - 2; i++) {
17+
if (i - 1 >= 0 && nums[i] == nums[i - 1]) {
18+
// Skip if this number is the same as the previous one,
19+
// so that we can avoid duplicate triplets.
20+
continue;
21+
}
22+
23+
int left = i + 1;
24+
int right = nums.length - 1;
25+
26+
while (left < right) {
27+
int sum = nums[i] + nums[left] + nums[right];
28+
if (sum == 0) {
29+
ArrayList<Integer> list = new ArrayList<>(
30+
Arrays.asList(nums[i], nums[left], nums[right]));
31+
answer.add(list);
32+
33+
// According to the problem, we need to avoid duplicate triplets.
34+
// Therefore, this loop is needed.
35+
while (left < right && nums[left] == nums[left + 1]) {
36+
left++;
37+
}
38+
while (left < right && nums[right] == nums[right - 1]) {
39+
right--;
40+
}
41+
42+
left++;
43+
right--;
44+
} else if (sum < 0) {
45+
left++;
46+
} else {
47+
right--;
48+
}
49+
}
50+
51+
}
52+
53+
return answer;
54+
}
55+
}

3sum/gcount85.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
# Intuition
3+
처음에는 1주차의 Two Sum 문제 풀이를 응용하여, 배열 순회 + 해시 테이블 만들어 값 찾기를 시도했으나
4+
정렬 + 투포인터 풀이가 더 빠르므로 그렇게 제출했습니다.
5+
6+
# Approach
7+
nums 배열을 순회하면서 -nums[i]를 합으로 하는 두 수를 나머지 배열 부분에서 찾는다.
8+
찾을 때는 정렬 & 투포인터를 활용하여 중복을 건너 뛰는 방식으로 속도를 높인다.
9+
10+
11+
# Complexity
12+
- Time complexity: 정렬 + 투포인터 이중 반복으로 O(N^2)
13+
14+
- Space complexity: 정렬 하는데에 O(N) , answer 배열 생성하는데에 O(M)
15+
"""
16+
17+
18+
class Solution:
19+
def threeSum(self, nums: list[int]) -> list[list[int]]:
20+
nums.sort() # O(NlogN)
21+
n = len(nums)
22+
answer = []
23+
24+
for i in range(n - 2): # 이중 반복문 O(N^2)
25+
# 중복 제거
26+
if i > 0 and nums[i] == nums[i - 1]:
27+
continue
28+
29+
# nums[i]가 0보다 크면 뒤도 다 양수라 종료 가능
30+
if nums[i] > 0:
31+
break
32+
33+
left, right = i + 1, n - 1
34+
35+
while left < right:
36+
total = nums[i] + nums[left] + nums[right]
37+
38+
if total == 0:
39+
answer.append([nums[i], nums[left], nums[right]])
40+
left += 1
41+
right -= 1
42+
43+
# left/right 중복 제거
44+
while left < right and nums[left] == nums[left - 1]:
45+
left += 1
46+
while left < right and nums[right] == nums[right + 1]:
47+
right -= 1
48+
49+
elif total < 0:
50+
left += 1
51+
else:
52+
right -= 1
53+
54+
return answer

3sum/hwi-middle.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> threeSum(vector<int>& nums) {
4+
vector<vector<int>> res;
5+
6+
sort(nums.begin(), nums.end());
7+
8+
for (int i = 0; i < nums.size() && nums[i] <= 0; ++i)
9+
{
10+
if (i != 0 && nums[i - 1] == nums[i])
11+
{
12+
continue;
13+
}
14+
15+
int l = i + 1;
16+
int r = nums.size() - 1;
17+
while (l < r)
18+
{
19+
int sum = nums[i] + nums[l] + nums[r];
20+
if (sum < 0)
21+
{
22+
++l;
23+
}
24+
else if (sum > 0)
25+
{
26+
--r;
27+
}
28+
else
29+
{
30+
res.push_back({nums[i], nums[l++], nums[r--]});
31+
while (l < r && nums[l] == nums[l - 1])
32+
{
33+
++l;
34+
}
35+
}
36+
}
37+
}
38+
39+
return res;
40+
}
41+
};

3sum/hyeri0903.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from itertools import combinations
2+
3+
class Solution:
4+
def threeSum(self, nums: List[int]) -> List[List[int]]:
5+
"""
6+
time complexity : O(n^2)
7+
space complexity : O(1)
8+
"""
9+
answer = []
10+
nums.sort()
11+
n = len(nums)
12+
13+
for i in range(n):
14+
#skipped if nums[i] == nums[i-1] to avoid duplicate triplets
15+
if i > 0 and nums[i] == nums[i-1]:
16+
continue
17+
18+
#search with two pointer
19+
left, right = i+1, n-1
20+
21+
while left < right:
22+
total = nums[left] + nums[i] + nums[right]
23+
if total == 0:
24+
answer.append([nums[left], nums[i], nums[right]])
25+
26+
#move the pointers past duplicates
27+
while left < right and nums[left] == nums[left+1]:
28+
left += 1
29+
while left < right and nums[right] == nums[right-1]:
30+
right -= 1
31+
32+
left += 1
33+
right -= 1
34+
elif total < 0:
35+
left += 1
36+
else:
37+
right -= 1
38+
39+
return answer

3sum/jiji-hoon96.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// time limit 실패
2+
3+
// function threeSum(nums: number[]): number[][] {
4+
// const result: number[][] = [];
5+
// for (let i = 0; i < nums.length; i++) {
6+
// for (let j = 1; j < nums.length; j++) {
7+
// for (let k = 2; k < nums.length; k++) {
8+
// if (
9+
// i !== j &&
10+
// j !== k &&
11+
// i !== k &&
12+
// nums[i] + nums[j] + nums[k] === 0
13+
// ) {
14+
// const hasArray = result.some(
15+
// (item) =>
16+
// JSON.stringify([...item].sort((a, b) => a - b)) ===
17+
// JSON.stringify([nums[i], nums[j], nums[k]].sort((a, b) => a - b)),
18+
// );
19+
// if (!hasArray) {
20+
// result.push([nums[i], nums[j], nums[k]]);
21+
// }
22+
// }
23+
// }
24+
// }
25+
// }
26+
// return result;
27+
// }
28+
29+
function threeSum(nums: number[]): number[][] {
30+
const result: number[][] = [];
31+
nums.sort((a, b) => a - b);
32+
33+
for (let i = 0; i < nums.length - 2; i++) {
34+
if (i > 0 && nums[i] === nums[i - 1]) continue;
35+
36+
let left = i + 1;
37+
let right = nums.length - 1;
38+
39+
while (left < right) {
40+
const sum = nums[i] + nums[left] + nums[right];
41+
42+
if (sum === 0) {
43+
result.push([nums[i], nums[left], nums[right]]);
44+
while (left < right && nums[left] === nums[left + 1]) left++;
45+
while (left < right && nums[right] === nums[right - 1]) right--;
46+
left++;
47+
right--;
48+
} else if (sum < 0) {
49+
left++;
50+
} else {
51+
right--;
52+
}
53+
}
54+
}
55+
return result;
56+
}
57+
58+
threeSum([-1, 0, 1, 2, -1, -4]); // [[-1,-1,2],[-1,0,1]]
59+
threeSum([0, 1, 1]); // []
60+
threeSum([0, 0, 0]); // [[0,0,0]]
61+
threeSum([
62+
12, 5, -12, 4, -11, 11, 2, 7, 2, -5, -14, -3, -3, 3, 2, -10, 9, -15, 2, 14,
63+
-3, -15, -3, -14, -1, -7, 11, -4, -11, 12, -15, -14, 2, 10, -2, -1, 6, 7, 13,
64+
-15, -13, 6, -10, -9, -14, 7, -12, 3, -1, 5, 2, 11, 6, 14, 12, -10, 14, 0, -7,
65+
11, -10, -7, 4, -1, -12, -13, 13, 1, 9, 3, 1, 3, -5, 6, 9, -4, -2, 5, 14, 12,
66+
-5, -6, 1, 8, -15, -10, 5, -15, -2, 5, 3, 3, 13, -8, -13, 8, -5, 8, -6, 11,
67+
-12, 3, 0, -2, -6, -14, 2, 0, 6, 1, -11, 9, 2, -3, -6, 3, 3, -15, -5, -14, 5,
68+
13, -4, -4, -10, -10, 11,
69+
]); // [[-15,1,14],[-15,2,13],[-15,3,12],[-15,4,11],[-15,5,10],[-15,6,9],[-15,7,8],[-14,0,14],[-14,1,13],[-14,2,12],[-14,3,11],[-14,4,10],[-14,5,9],[-14,6,8],[-14,7,7],[-13,-1,14],[-13,0,13],[-13,1,12],[-13,2,11],[-13,3,10],[-13,4,9],[-13,5,8],[-13,6,7],[-12,-2,14],[-12,-1,13],[-12,0,12],[-12,1,11],[-12,2,10],[-12,3,9],[-12,4,8],[-12,5,7],[-12,6,6],[-11,-3,14],[-11,-2,13],[-11,-1,12],[-11,0,11],[-11,1,10],[-11,2,9],[-11,3,8],[-11,4,7],[-11,5,6],[-10,-4,14],[-10,-3,13],[-10,-2,12],[-10,-1,11],[-10,0,10],[-10,1,9],[-10,2,8],[-10,3,7],[-10,4,6],[-10,5,5],[-9,-5,14],[-9,-4,13],[-9,-3,12],[-9,-2,11],[-9,-1,10],[-9,0,9],[-9,1,8],[-9,2,7],[-9,3,6],[-9,4,5],[-8,-6,14],[-8,-5,13],[-8,-4,12],[-8,-3,11],[-8,-2,10],[-8,-1,9],[-8,0,8],[-8,1,7],[-8,2,6],[-8,3,5],[-8,4,4],[-7,-7,14],[-7,-6,13],[-7,-5,12],[-7,-4,11],[-7,-3,10],[-7,-2,9],[-7,-1,8],[-7,0,7],[-7,1,6],[-7,2,5],[-7,3,4],[-6,-6,12],[-6,-5,11],[-6,-4,10],[-6,-3,9],[-6,-2,8],[-6,-1,7],[-6,0,6],[-6,1,5],[-6,2,4],[-6,3,3],[-5,-5,10],[-5,-4,9],[-5,-3,8...

0 commit comments

Comments
 (0)