Skip to content

Commit ed5382d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 3057d82 + 109a279 commit ed5382d

File tree

114 files changed

+4367
-0
lines changed

Some content is hidden

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

114 files changed

+4367
-0
lines changed

โ€Ž3sum/Chaedie.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
์ฒซ๋ฒˆ์งธ ํ’€์ด -> ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ ํ’€์ด
3+
1) sort์™€ two pointer๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด
4+
2) has_set ์„ ํ™œ์šฉํ•œ ์ค‘๋ณต ์ œ๊ฑฐ
5+
6+
๋‘๋ฒˆ์งธ ํ’€์ด -> Neetcode ํ’€์ด
7+
1) sort์™€ two pointer๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด
8+
2) while loop ๋ฅผ ํ™œ์šฉํ•œ ์ค‘๋ณต ์ œ๊ฑฐ
9+
10+
Time: O(n^2) = O(n) * O(n)
11+
Space: O(n)
12+
"""
13+
14+
15+
class Solution:
16+
def threeSum(self, nums: List[int]) -> List[List[int]]:
17+
nums.sort()
18+
res = set()
19+
n = len(nums)
20+
21+
for i in range(n):
22+
l, r = i + 1, n - 1
23+
while l < r:
24+
summ = nums[i] + nums[l] + nums[r]
25+
if summ < 0:
26+
l += 1
27+
elif summ > 0:
28+
r -= 1
29+
else:
30+
res.add((nums[i], nums[l], nums[r]))
31+
l += 1
32+
return list(res)
33+
34+
35+
class Solution:
36+
def threeSum(self, nums: List[int]) -> List[List[int]]:
37+
nums.sort()
38+
res = []
39+
n = len(nums)
40+
41+
for i in range(n):
42+
l, r = i + 1, n - 1
43+
44+
if i > 0 and nums[i] == nums[i - 1]:
45+
continue
46+
47+
while l < r:
48+
summ = nums[i] + nums[l] + nums[r]
49+
if summ < 0:
50+
l += 1
51+
elif summ > 0:
52+
r -= 1
53+
else:
54+
res.append([nums[i], nums[l], nums[r]])
55+
l += 1
56+
while nums[l] == nums[l - 1] and l < r:
57+
l += 1
58+
59+
return res

โ€Ž3sum/GangBean.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
/**
4+
1. understanding
5+
- integer array nums, find the whole combination of 3 nums, and the sum of the 3 nums equal to 0. And don't allow reusing same indiced number(but can duplicate in value)
6+
2. solve strategy
7+
- brute force
8+
- in every combination, validate sum of the nums equal to 0
9+
- but it can take O(N^3) times where N is the length of input array, and given that the N can be 3000 at most(3 * 10^3), time can be 27 * 10^9, which takes too long...
10+
- sort and two pointers
11+
- sort nums in ascending order, so move the left pointer to right means the sum of window is getting bigger.
12+
- and mid pointer set to left + 1 index
13+
- if sum of pointers is less than 0, then move mid pointer to right, until the sum is bigger than 0, and while processing them, if the sum of pointers is 0, then add the combination to the return list.
14+
- [-4, -1, -1, 0, 1, 2]:
15+
16+
3. complexity
17+
- time: O(N^2) -> each left pointer, you can search at most N-1, and left pointer's range is [0, N-1), so the max length is N-1 for left index pointer.
18+
- space: O(1) -> no extra space is needed
19+
*/
20+
// 0. assign return variable Set
21+
Set<List<Integer>> answer = new HashSet<>();
22+
23+
// 1. sort the num array in ascending order
24+
Arrays.sort(nums); // O(NlogN)
25+
// Arrays.stream(nums).forEach(System.out::println);
26+
27+
// 3. move the mid pointer from left to right to find the combination of which's sum is 0, and if the sum is over 0, and then move right pointer to the left. else if the sum is under 0, then move left pointer to right direction.
28+
for (int left = 0; left < nums.length - 1; left++) {
29+
int mid = left + 1;
30+
int right = nums.length - 1;
31+
while (mid < right) {
32+
// System.out.println(String.format("%d,%d,%d", nums[left], nums[mid], nums[right]));
33+
int sum = nums[left] + nums[mid] + nums[right];
34+
if (sum > 0) {
35+
right--;
36+
} else if (sum == 0) {
37+
answer.add(List.of(nums[left], nums[mid], nums[right]));
38+
right--;
39+
} else {
40+
mid++;
41+
}
42+
}
43+
}
44+
45+
return new ArrayList<>(answer);
46+
}
47+
}
48+

โ€Ž3sum/Gotprgmer.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ์ด์ „์— ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹œ๋„ํ–ˆ์ง€๋งŒ ์ค‘๋ณต๋œ ๊ฐ’๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
2+
// ๊ทธ๋ž˜์„œ ํ•ด๋‹ต์„ ๋ณด์•˜๊ณ  ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
3+
// ์„œ๋กœ ๋‹ค๋ฅธ i์™€ j ์ธ๋ฑ์Šค๋ฅผ 2์ค‘ for๋ฌธ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด์„œ
4+
// i์™€ j์‚ฌ์ด ์ˆ˜๋“ค์„ set์œผ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
5+
// set์— -nums[i]-nums[j]๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
6+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N^2)
7+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(N)
8+
class SolutionGotprgmer {
9+
public List<List<Integer>> threeSum(int[] nums) {
10+
// ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
11+
List<List<Integer>> result = new ArrayList<>();
12+
Set<Integer> set;
13+
Set<List<Integer>> resultSet = new HashSet<>();
14+
List<Integer> numList;
15+
16+
17+
// ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ
18+
Arrays.sort(nums);
19+
for(int i=0;i<nums.length-2;i++){
20+
if (i > 0 && nums[i - 1] == nums[i]) continue;
21+
22+
set = new HashSet<>();
23+
for(int j=i+1;j<nums.length;j++){
24+
int checkNum = nums[i]+nums[j];
25+
if(set.contains(-checkNum)){
26+
numList = new ArrayList<>(Arrays.asList(nums[i], -checkNum, nums[j]));
27+
if(!resultSet.contains(numList)){
28+
result.add(numList);
29+
resultSet.add(numList);
30+
}
31+
}
32+
set.add(nums[j]);
33+
}
34+
}
35+
return result;
36+
}
37+
}

โ€Ž3sum/HodaeSsi.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
answerSet = set()
4+
nums.sort()
5+
6+
for i in range(len(nums) - 2):
7+
leftIdx = i + 1
8+
rightIdx = len(nums) - 1
9+
while leftIdx < rightIdx:
10+
sum = nums[i] + nums[leftIdx] + nums[rightIdx]
11+
if sum < 0:
12+
leftIdx += 1
13+
elif sum > 0:
14+
rightIdx -= 1
15+
else:
16+
answerSet.add((nums[i], nums[leftIdx], nums[rightIdx]))
17+
leftIdx = leftIdx + 1
18+
rightIdx = rightIdx - 1
19+
20+
return list(answerSet)
21+

โ€Ž3sum/JisooPyo.kt

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
/**
7+
* Leetcode
8+
* 15. 3Sum
9+
* Medium
10+
*/
11+
class `3Sum` {
12+
/**
13+
* 3์ค‘ for๋ฌธ์œผ๋กœ ํ’€์–ด๋ดค๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋˜๋”๋ผ๊ณ ์š”(๋‹น์—ฐ)
14+
* ์‚ฌ์‹ค ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ Topic๊ณผ ํžŒํŠธ๋ฅผ ์‚ด์ง ๋ดค๋Š”๋ฐ ํˆฌ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ๊ธธ๋ž˜ ์ด๊ฑธ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค!
15+
*
16+
* Runtime: 72 ms(Beats: 60.54 %)
17+
* Time Complexity: O(n^2)
18+
*
19+
* Memory: 56.28 MB(Beats: 49.51 %)
20+
* Space Complexity: O(n^2)
21+
*/
22+
fun threeSum(nums: IntArray): List<List<Int>> {
23+
val answer = mutableListOf<List<Int>>()
24+
// ๋ฐฐ์—ด ์ •๋ ฌ - ์ค‘๋ณต๋œ ๊ฒฝ์šฐ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ
25+
nums.sort()
26+
27+
// nums[i]์˜ ์ด์ „ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
28+
var prev = Integer.MIN_VALUE
29+
for (i in nums.indices) {
30+
// ์ด์ „ ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฐ’์ด๋ผ๋ฉด ์Šคํ‚ตํ•˜์—ฌ ์ค‘๋ณต๋œ ๊ฒฝ์šฐ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
31+
if (nums[i] == prev) {
32+
continue
33+
}
34+
35+
// ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ๋‹ค ๋”ํ•˜์—ฌ 0์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
36+
var left = i + 1
37+
var right = nums.size - 1
38+
while (left < right) {
39+
40+
if (nums[i] + nums[left] + nums[right] > 0) { // ํ•ฉ์ด 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด right๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค.
41+
// if ๋‚ด์— ์žˆ๋Š” while๋ฌธ๋“ค์€ ์ค‘๋ณต ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.
42+
while (0 <= right - 1 && nums[right - 1] == nums[right]) {
43+
right--
44+
}
45+
right--
46+
} else if (nums[i] + nums[left] + nums[right] < 0) { // ํ•ฉ์ด 0๋ณด๋‹ค ์ ๋‹ค๋ฉด left๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค.
47+
while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) {
48+
left++
49+
}
50+
left++
51+
} else { // ํ•ฉ์ด 0์ด๋ผ๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
52+
answer.add(listOf(nums[i], nums[left], nums[right]))
53+
while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) {
54+
left++
55+
}
56+
left++
57+
while (0 <= right - 1 && nums[right - 1] == nums[right]) {
58+
right--
59+
}
60+
right--
61+
}
62+
}
63+
prev = nums[i]
64+
}
65+
return answer
66+
}
67+
68+
/**
69+
* ์‹œ๊ฐ„ ๋ณต์žก๋„๋‚˜ ๊ณต๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฐœ์„ ๋˜์ง„ ์•Š์•˜์ง€๋งŒ ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ ๊ฐœ์„ ํ•ด๋ณธ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.
70+
* Runtime: 66 ms(Beats: 65.59 %)
71+
* Time Complexity: O(n^2)
72+
*
73+
* Memory: 56.64 MB(Beats: 43.12 %)
74+
* Space Complexity: O(n^2)
75+
*/
76+
fun threeSum2(nums: IntArray): List<List<Int>> {
77+
val answer = mutableListOf<List<Int>>()
78+
nums.sort()
79+
80+
// ์ฒซ ์„ธ ์ˆ˜์˜ ํ•ฉ์ด 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ๋งˆ์ง€๋ง‰ ์„ธ ์ˆ˜์˜ ํ•ฉ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ถˆ๊ฐ€๋Šฅ
81+
val lastIndex = nums.size - 1
82+
if (nums[0] + nums[1] + nums[2] > 0 ||
83+
nums[lastIndex] + nums[lastIndex - 1] + nums[lastIndex - 2] < 0
84+
) {
85+
return emptyList()
86+
}
87+
88+
var prev = nums[0] - 1
89+
for (i in nums.indices) {
90+
// ์กฐ๊ธฐ ์ข…๋ฃŒ ์กฐ๊ฑด ์ถ”๊ฐ€
91+
if (nums[i] > 0) {
92+
break
93+
}
94+
if (nums[i] == prev) {
95+
continue
96+
}
97+
var left = i + 1
98+
var right = nums.size - 1
99+
while (left < right) {
100+
// ์ค‘๋ณต ๋กœ์ง ์ œ๊ฑฐ ๋ฐ sum ๋ณ€์ˆ˜ํ™”
101+
val sum = nums[i] + nums[left] + nums[right]
102+
when {
103+
sum > 0 -> right = skipDuplicates(nums, right, false)
104+
sum < 0 -> left = skipDuplicates(nums, left, true)
105+
else -> {
106+
answer.add(listOf(nums[i], nums[left], nums[right]))
107+
left = skipDuplicates(nums, left, true)
108+
right = skipDuplicates(nums, right, false)
109+
}
110+
}
111+
}
112+
prev = nums[i]
113+
}
114+
return answer
115+
}
116+
117+
private fun skipDuplicates(nums: IntArray, index: Int, isLeft: Boolean): Int {
118+
var current = index
119+
return if (isLeft) {
120+
while (current + 1 < nums.size && nums[current] == nums[current + 1]) current++
121+
current + 1
122+
} else {
123+
while (0 <= current - 1 && nums[current - 1] == nums[current]) current--
124+
current - 1
125+
}
126+
}
127+
128+
@Test
129+
fun test() {
130+
threeSum(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf(
131+
listOf(-1, -1, 2),
132+
listOf(-1, 0, 1)
133+
)
134+
threeSum(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>()
135+
threeSum(intArrayOf(0, 0, 0)) shouldBe listOf(
136+
listOf(0, 0, 0)
137+
)
138+
threeSum(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf(
139+
listOf(-2, 0, 2)
140+
)
141+
threeSum2(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf(
142+
listOf(-1, -1, 2),
143+
listOf(-1, 0, 1)
144+
)
145+
threeSum2(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>()
146+
threeSum2(intArrayOf(0, 0, 0)) shouldBe listOf(
147+
listOf(0, 0, 0)
148+
)
149+
threeSum2(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf(
150+
listOf(-2, 0, 2)
151+
)
152+
}
153+
154+
}

โ€Ž3sum/KwonNayeon.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Constraints:
3+
1. 3 <= nums.length <= 3000
4+
2. -10^5 <= nums[i] <= 10^5
5+
6+
Time Complexity:
7+
- O(n^2) (์ •๋ ฌ์€ O(n log n), ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์€ O(n^2))
8+
Space Complexity:
9+
- O(n) (๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ)
10+
"""
11+
12+
class Solution:
13+
def threeSum(self, nums: List[int]) -> List[List[int]]:
14+
nums.sort()
15+
result = []
16+
17+
for i in range(len(nums) - 2):
18+
if i > 0 and nums[i] == nums[i-1]:
19+
continue
20+
21+
left, right = i+1, len(nums)-1
22+
23+
while left < right:
24+
sum = nums[i] + nums[left] + nums[right]
25+
26+
if sum == 0:
27+
result.append([nums[i], nums[left], nums[right]])
28+
29+
while left < right and nums[left] == nums[left+1]:
30+
left += 1
31+
while left < right and nums[right] == nums[right-1]:
32+
right -= 1
33+
34+
left += 1
35+
right -= 1
36+
37+
elif sum < 0:
38+
left += 1
39+
else:
40+
right -= 1
41+
42+
return result

โ€Ž3sum/aa601.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ์ „์ฒด ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
2+
class Solution:
3+
def threeSum(self, nums: list[int]) -> list[list[int]]:
4+
nums.sort() # ๋‚ด์žฅํ•จ์ˆ˜ sort()์˜ ์‹œ๊ฐ„๋ณต์žก๋„ O(nlogn)
5+
result = [] # ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
6+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
7+
for i in range(len(nums) - 2):
8+
if i > 0 and nums[i] == nums[i - 1]:
9+
continue
10+
r = len(nums) - 1
11+
l = i + 1
12+
# i๋ฅผ ๊ธฐ์ค€์œผ๋กœ l๊ณผ r์„ ํƒ์ƒ‰ํ•˜๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
13+
while l < r:
14+
if nums[i] + nums[l] + nums[r] < 0:
15+
l += 1
16+
elif nums[i] + nums[l] + nums[r] > 0:
17+
r -= 1
18+
else:
19+
result.append([nums[i], nums[l], nums[r]])
20+
while l < r and nums[l] == nums[l + 1]: # ์ค‘๋ณต ์ œ๊ฑฐ ๋ฐ˜๋ณต๋ฌธ, ์ด๋ฏธ ์ง„ํ–‰๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํƒ์ƒ‰ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ถ”๊ฐ€๋˜์ง€ ์•Š์Œ
21+
l += 1
22+
while l < r and nums[r] == nums[r - 1]:
23+
r -= 1
24+
l += 1
25+
r -= 1
26+
return result
27+

0 commit comments

Comments
ย (0)