Skip to content

Commit 7f9334a

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 9971958 + 28bf5d3 commit 7f9334a

403 files changed

Lines changed: 10155 additions & 678 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

0 commit comments

Comments
 (0)