Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ysle0 authored Dec 22, 2024
2 parents 3057d82 + 109a279 commit ed5382d
Show file tree
Hide file tree
Showing 114 changed files with 4,367 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 3sum/Chaedie.py
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
48 changes: 48 additions & 0 deletions 3sum/GangBean.java
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);
}
}

37 changes: 37 additions & 0 deletions 3sum/Gotprgmer.java
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;
}
}
21 changes: 21 additions & 0 deletions 3sum/HodaeSsi.py
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)

154 changes: 154 additions & 0 deletions 3sum/JisooPyo.kt
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)
)
}

}
42 changes: 42 additions & 0 deletions 3sum/KwonNayeon.py
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
27 changes: 27 additions & 0 deletions 3sum/aa601.py
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

Loading

0 comments on commit ed5382d

Please sign in to comment.