-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
114 changed files
with
4,367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
์ฒซ๋ฒ์งธ ํ์ด -> ๋ฌ๋ ์ ์ฝ๋ ํ์ด | ||
1) sort์ two pointer๋ฅผ ํ์ฉํ ํ์ด | ||
2) has_set ์ ํ์ฉํ ์ค๋ณต ์ ๊ฑฐ | ||
๋๋ฒ์งธ ํ์ด -> Neetcode ํ์ด | ||
1) sort์ two pointer๋ฅผ ํ์ฉํ ํ์ด | ||
2) while loop ๋ฅผ ํ์ฉํ ์ค๋ณต ์ ๊ฑฐ | ||
Time: O(n^2) = O(n) * O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
res = set() | ||
n = len(nums) | ||
|
||
for i in range(n): | ||
l, r = i + 1, n - 1 | ||
while l < r: | ||
summ = nums[i] + nums[l] + nums[r] | ||
if summ < 0: | ||
l += 1 | ||
elif summ > 0: | ||
r -= 1 | ||
else: | ||
res.add((nums[i], nums[l], nums[r])) | ||
l += 1 | ||
return list(res) | ||
|
||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
res = [] | ||
n = len(nums) | ||
|
||
for i in range(n): | ||
l, r = i + 1, n - 1 | ||
|
||
if i > 0 and nums[i] == nums[i - 1]: | ||
continue | ||
|
||
while l < r: | ||
summ = nums[i] + nums[l] + nums[r] | ||
if summ < 0: | ||
l += 1 | ||
elif summ > 0: | ||
r -= 1 | ||
else: | ||
res.append([nums[i], nums[l], nums[r]]) | ||
l += 1 | ||
while nums[l] == nums[l - 1] and l < r: | ||
l += 1 | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
/** | ||
1. understanding | ||
- 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) | ||
2. solve strategy | ||
- brute force | ||
- in every combination, validate sum of the nums equal to 0 | ||
- 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... | ||
- sort and two pointers | ||
- sort nums in ascending order, so move the left pointer to right means the sum of window is getting bigger. | ||
- and mid pointer set to left + 1 index | ||
- 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. | ||
- [-4, -1, -1, 0, 1, 2]: | ||
3. complexity | ||
- 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. | ||
- space: O(1) -> no extra space is needed | ||
*/ | ||
// 0. assign return variable Set | ||
Set<List<Integer>> answer = new HashSet<>(); | ||
|
||
// 1. sort the num array in ascending order | ||
Arrays.sort(nums); // O(NlogN) | ||
// Arrays.stream(nums).forEach(System.out::println); | ||
|
||
// 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. | ||
for (int left = 0; left < nums.length - 1; left++) { | ||
int mid = left + 1; | ||
int right = nums.length - 1; | ||
while (mid < right) { | ||
// System.out.println(String.format("%d,%d,%d", nums[left], nums[mid], nums[right])); | ||
int sum = nums[left] + nums[mid] + nums[right]; | ||
if (sum > 0) { | ||
right--; | ||
} else if (sum == 0) { | ||
answer.add(List.of(nums[left], nums[mid], nums[right])); | ||
right--; | ||
} else { | ||
mid++; | ||
} | ||
} | ||
} | ||
|
||
return new ArrayList<>(answer); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// ์ด์ ์ ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํ์ฌ ์๋ํ์ง๋ง ์ค๋ณต๋ ๊ฐ๋ค์ ์ฒ๋ฆฌํ๋๋ฐ ์ด๋ ค์์ด ์์์ต๋๋ค. | ||
// ๊ทธ๋์ ํด๋ต์ ๋ณด์๊ณ ์๋ก์ด ๋ฐฉ๋ฒ์ผ๋ก ํ์ด๋ณด์์ต๋๋ค. | ||
// ์๋ก ๋ค๋ฅธ i์ j ์ธ๋ฑ์ค๋ฅผ 2์ค for๋ฌธ์ผ๋ก ์งํํ๋ฉด์ | ||
// i์ j์ฌ์ด ์๋ค์ set์ผ๋ก ๊ด๋ฆฌํฉ๋๋ค. | ||
// set์ -nums[i]-nums[j]๊ฐ ์กด์ฌํ๋ฉด ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐํฉ๋๋ค. | ||
// ์๊ฐ๋ณต์ก๋ : O(N^2) | ||
// ๊ณต๊ฐ๋ณต์ก๋ : O(N) | ||
class SolutionGotprgmer { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
// ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Set<Integer> set; | ||
Set<List<Integer>> resultSet = new HashSet<>(); | ||
List<Integer> numList; | ||
|
||
|
||
// ๋ฆฌ์คํธ ์ ๋ ฌ | ||
Arrays.sort(nums); | ||
for(int i=0;i<nums.length-2;i++){ | ||
if (i > 0 && nums[i - 1] == nums[i]) continue; | ||
|
||
set = new HashSet<>(); | ||
for(int j=i+1;j<nums.length;j++){ | ||
int checkNum = nums[i]+nums[j]; | ||
if(set.contains(-checkNum)){ | ||
numList = new ArrayList<>(Arrays.asList(nums[i], -checkNum, nums[j])); | ||
if(!resultSet.contains(numList)){ | ||
result.add(numList); | ||
resultSet.add(numList); | ||
} | ||
} | ||
set.add(nums[j]); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
answerSet = set() | ||
nums.sort() | ||
|
||
for i in range(len(nums) - 2): | ||
leftIdx = i + 1 | ||
rightIdx = len(nums) - 1 | ||
while leftIdx < rightIdx: | ||
sum = nums[i] + nums[leftIdx] + nums[rightIdx] | ||
if sum < 0: | ||
leftIdx += 1 | ||
elif sum > 0: | ||
rightIdx -= 1 | ||
else: | ||
answerSet.add((nums[i], nums[leftIdx], nums[rightIdx])) | ||
leftIdx = leftIdx + 1 | ||
rightIdx = rightIdx - 1 | ||
|
||
return list(answerSet) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* Leetcode | ||
* 15. 3Sum | ||
* Medium | ||
*/ | ||
class `3Sum` { | ||
/** | ||
* 3์ค for๋ฌธ์ผ๋ก ํ์ด๋ดค๋๋ฐ ์๊ฐ ์ด๊ณผ๊ฐ ๋๋๋ผ๊ณ ์(๋น์ฐ) | ||
* ์ฌ์ค ์ ๋ชจ๋ฅด๊ฒ ์ด์ Topic๊ณผ ํํธ๋ฅผ ์ด์ง ๋ดค๋๋ฐ ํฌ ํฌ์ธํฐ๊ฐ ์๊ธธ๋ ์ด๊ฑธ ์ด์ฉํด์ ํ์ด๋ณด๊ธฐ๋ก ํ์ต๋๋ค! | ||
* | ||
* Runtime: 72 ms(Beats: 60.54 %) | ||
* Time Complexity: O(n^2) | ||
* | ||
* Memory: 56.28 MB(Beats: 49.51 %) | ||
* Space Complexity: O(n^2) | ||
*/ | ||
fun threeSum(nums: IntArray): List<List<Int>> { | ||
val answer = mutableListOf<List<Int>>() | ||
// ๋ฐฐ์ด ์ ๋ ฌ - ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํ๊ณ ํจ์จ์ ์ผ๋ก ํ์ํ๊ธฐ ์ํ์ฌ | ||
nums.sort() | ||
|
||
// nums[i]์ ์ด์ ๊ฐ์ ์๋ฏธํฉ๋๋ค. | ||
var prev = Integer.MIN_VALUE | ||
for (i in nums.indices) { | ||
// ์ด์ ๊ฐ๊ณผ ๋์ผํ ๊ฐ์ด๋ผ๋ฉด ์คํตํ์ฌ ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํฉ๋๋ค. | ||
if (nums[i] == prev) { | ||
continue | ||
} | ||
|
||
// ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์ฌ ๋ค ๋ํ์ฌ 0์ด ๋๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์ต๋๋ค. | ||
var left = i + 1 | ||
var right = nums.size - 1 | ||
while (left < right) { | ||
|
||
if (nums[i] + nums[left] + nums[right] > 0) { // ํฉ์ด 0๋ณด๋ค ํฌ๋ค๋ฉด right๋ฅผ ์ค์ ๋๋ค. | ||
// if ๋ด์ ์๋ while๋ฌธ๋ค์ ์ค๋ณต ๊ฒฝ์ฐ์ ์๋ฅผ ํผํ๊ธฐ ์ํจ์ ๋๋ค. | ||
while (0 <= right - 1 && nums[right - 1] == nums[right]) { | ||
right-- | ||
} | ||
right-- | ||
} else if (nums[i] + nums[left] + nums[right] < 0) { // ํฉ์ด 0๋ณด๋ค ์ ๋ค๋ฉด left๋ฅผ ๋์ ๋๋ค. | ||
while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) { | ||
left++ | ||
} | ||
left++ | ||
} else { // ํฉ์ด 0์ด๋ผ๋ฉด ๊ฒฝ์ฐ์ ์๋ฅผ ์ถ๊ฐํฉ๋๋ค. | ||
answer.add(listOf(nums[i], nums[left], nums[right])) | ||
while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) { | ||
left++ | ||
} | ||
left++ | ||
while (0 <= right - 1 && nums[right - 1] == nums[right]) { | ||
right-- | ||
} | ||
right-- | ||
} | ||
} | ||
prev = nums[i] | ||
} | ||
return answer | ||
} | ||
|
||
/** | ||
* ์๊ฐ ๋ณต์ก๋๋ ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ๊ฐ์ ๋์ง ์์์ง๋ง ๊ฐ๋ ์ฑ ์ธก๋ฉด์์ ๊ฐ์ ํด๋ณธ ๋ฒ์ ์ ๋๋ค. | ||
* Runtime: 66 ms(Beats: 65.59 %) | ||
* Time Complexity: O(n^2) | ||
* | ||
* Memory: 56.64 MB(Beats: 43.12 %) | ||
* Space Complexity: O(n^2) | ||
*/ | ||
fun threeSum2(nums: IntArray): List<List<Int>> { | ||
val answer = mutableListOf<List<Int>>() | ||
nums.sort() | ||
|
||
// ์ฒซ ์ธ ์์ ํฉ์ด 0๋ณด๋ค ํฌ๊ฑฐ๋, ๋ง์ง๋ง ์ธ ์์ ํฉ์ด 0๋ณด๋ค ์์ผ๋ฉด ๋ถ๊ฐ๋ฅ | ||
val lastIndex = nums.size - 1 | ||
if (nums[0] + nums[1] + nums[2] > 0 || | ||
nums[lastIndex] + nums[lastIndex - 1] + nums[lastIndex - 2] < 0 | ||
) { | ||
return emptyList() | ||
} | ||
|
||
var prev = nums[0] - 1 | ||
for (i in nums.indices) { | ||
// ์กฐ๊ธฐ ์ข ๋ฃ ์กฐ๊ฑด ์ถ๊ฐ | ||
if (nums[i] > 0) { | ||
break | ||
} | ||
if (nums[i] == prev) { | ||
continue | ||
} | ||
var left = i + 1 | ||
var right = nums.size - 1 | ||
while (left < right) { | ||
// ์ค๋ณต ๋ก์ง ์ ๊ฑฐ ๋ฐ sum ๋ณ์ํ | ||
val sum = nums[i] + nums[left] + nums[right] | ||
when { | ||
sum > 0 -> right = skipDuplicates(nums, right, false) | ||
sum < 0 -> left = skipDuplicates(nums, left, true) | ||
else -> { | ||
answer.add(listOf(nums[i], nums[left], nums[right])) | ||
left = skipDuplicates(nums, left, true) | ||
right = skipDuplicates(nums, right, false) | ||
} | ||
} | ||
} | ||
prev = nums[i] | ||
} | ||
return answer | ||
} | ||
|
||
private fun skipDuplicates(nums: IntArray, index: Int, isLeft: Boolean): Int { | ||
var current = index | ||
return if (isLeft) { | ||
while (current + 1 < nums.size && nums[current] == nums[current + 1]) current++ | ||
current + 1 | ||
} else { | ||
while (0 <= current - 1 && nums[current - 1] == nums[current]) current-- | ||
current - 1 | ||
} | ||
} | ||
|
||
@Test | ||
fun test() { | ||
threeSum(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf( | ||
listOf(-1, -1, 2), | ||
listOf(-1, 0, 1) | ||
) | ||
threeSum(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>() | ||
threeSum(intArrayOf(0, 0, 0)) shouldBe listOf( | ||
listOf(0, 0, 0) | ||
) | ||
threeSum(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf( | ||
listOf(-2, 0, 2) | ||
) | ||
threeSum2(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf( | ||
listOf(-1, -1, 2), | ||
listOf(-1, 0, 1) | ||
) | ||
threeSum2(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>() | ||
threeSum2(intArrayOf(0, 0, 0)) shouldBe listOf( | ||
listOf(0, 0, 0) | ||
) | ||
threeSum2(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf( | ||
listOf(-2, 0, 2) | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Constraints: | ||
1. 3 <= nums.length <= 3000 | ||
2. -10^5 <= nums[i] <= 10^5 | ||
Time Complexity: | ||
- O(n^2) (์ ๋ ฌ์ O(n log n), ์ด์ค ๋ฐ๋ณต๋ฌธ์ O(n^2)) | ||
Space Complexity: | ||
- O(n) (๊ฒฐ๊ณผ ๋ฆฌ์คํธ) | ||
""" | ||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
result = [] | ||
|
||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i] == nums[i-1]: | ||
continue | ||
|
||
left, right = i+1, len(nums)-1 | ||
|
||
while left < right: | ||
sum = nums[i] + nums[left] + nums[right] | ||
|
||
if sum == 0: | ||
result.append([nums[i], nums[left], nums[right]]) | ||
|
||
while left < right and nums[left] == nums[left+1]: | ||
left += 1 | ||
while left < right and nums[right] == nums[right-1]: | ||
right -= 1 | ||
|
||
left += 1 | ||
right -= 1 | ||
|
||
elif sum < 0: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# ์ ์ฒด ์๊ฐ๋ณต์ก๋ : O(n^2), ๊ณต๊ฐ๋ณต์ก๋ : O(n) | ||
class Solution: | ||
def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
nums.sort() # ๋ด์ฅํจ์ sort()์ ์๊ฐ๋ณต์ก๋ O(nlogn) | ||
result = [] # ๊ณต๊ฐ๋ณต์ก๋ O(n) | ||
# ์๊ฐ๋ณต์ก๋ O(n) | ||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i] == nums[i - 1]: | ||
continue | ||
r = len(nums) - 1 | ||
l = i + 1 | ||
# i๋ฅผ ๊ธฐ์ค์ผ๋ก l๊ณผ r์ ํ์ํ๋ ์๊ฐ ๋ณต์ก๋ : O(n) | ||
while l < r: | ||
if nums[i] + nums[l] + nums[r] < 0: | ||
l += 1 | ||
elif nums[i] + nums[l] + nums[r] > 0: | ||
r -= 1 | ||
else: | ||
result.append([nums[i], nums[l], nums[r]]) | ||
while l < r and nums[l] == nums[l + 1]: # ์ค๋ณต ์ ๊ฑฐ ๋ฐ๋ณต๋ฌธ, ์ด๋ฏธ ์งํ๋ ๋ฆฌ์คํธ๋ฅผ ๋ค์ ํ์ํ์ง ์์ผ๋ฏ๋ก ์๊ฐ๋ณต์ก๋๋ ์ถ๊ฐ๋์ง ์์ | ||
l += 1 | ||
while l < r and nums[r] == nums[r - 1]: | ||
r -= 1 | ||
l += 1 | ||
r -= 1 | ||
return result | ||
|
Oops, something went wrong.