Skip to content

Commit 1f732f5

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents f70016b + 85a306c commit 1f732f5

70 files changed

Lines changed: 2119 additions & 23 deletions

Some content is hidden

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

โ€Ž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/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/junzero741.tsโ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// TC: O(n^2)
2+
// SC: O(logn)
3+
function threeSum(nums: number[]): number[][] {
4+
5+
nums.sort((a,b) => a-b);
6+
const result: number[][] = []
7+
8+
for(let i = 0 ; i < nums.length; i++) {
9+
let left = i+1;
10+
let right = nums.length-1;
11+
12+
while(left < right) {
13+
if(i > 0 && nums[i] === nums[i-1]) {
14+
break;
15+
}
16+
17+
const sum = nums[i] + nums[left] + nums[right];
18+
if(sum > 0) {
19+
right--;
20+
continue;
21+
}
22+
if(sum < 0) {
23+
left++;
24+
continue;
25+
}
26+
if(sum === 0) {
27+
const triplet: number[] = [nums[i], nums[left], nums[right]];
28+
result.push(triplet);
29+
30+
while(left < right && nums[left] === nums[left+1]) {
31+
left++;
32+
}
33+
while(left < right && nums[right] === nums[right-1]) {
34+
right--;
35+
}
36+
37+
left++;
38+
right--;
39+
}
40+
}
41+
42+
}
43+
44+
return result;
45+
};

โ€Ž3sum/mrlee7.pyโ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
result: List[List[int]] = []
7+
nums.sort()
8+
9+
for i in range(len(nums) - 2):
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+
result.append([nums[i], nums[left], nums[right]])
21+
left += 1
22+
right -= 1
23+
24+
while left < right and nums[left] == nums[left - 1]:
25+
left += 1
26+
while left < right and nums[right] == nums[right + 1]:
27+
right -= 1
28+
29+
elif total < 0:
30+
left += 1
31+
else:
32+
right -= 1
33+
34+
return result

โ€Žclimbing-stairs/DaleSeo.rsโ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn climb_stairs(n: i32) -> i32 {
5+
if n < 3 {
6+
return n;
7+
}
8+
let (mut pre, mut cur) = (1, 2);
9+
for _ in 3..=n {
10+
let next = pre + cur;
11+
pre = cur;
12+
cur = next;
13+
}
14+
cur
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int climbStairs(int n) {
5+
int[] steps = new int[n+1];
6+
if (n == 1) return 1;
7+
if (n == 2) return 2;
8+
if (n == 3) return 3;
9+
steps[1] = 1; steps[2] = 2;//2, 1+1
10+
steps[3] = 3;
11+
for(int i = 4;i < n+1; i++){
12+
steps[i] = steps[i-1] + steps[i-2];
13+
}
14+
/*
15+
steps[4] =
16+
1+1+1+1
17+
1+2+1
18+
2+2
19+
2+1+1
20+
1+1+2
21+
*/
22+
23+
return steps[n];
24+
25+
26+
}
27+
}
28+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
You are climbing a staircase. It takes n steps to reach the top.
3+
4+
Each time you can either climb 1 or 2 steps.
5+
In how many distinct ways can you climb to the top?
6+
7+
Example 1:
8+
9+
Input: n = 2
10+
Output: 2
11+
Explanation: There are two ways to climb to the top.
12+
1. 1 step + 1 step
13+
2. 2 steps
14+
15+
Constraints:
16+
1 <= n <= 45
17+
*/
18+
19+
// TimeComplexity : O(n)
20+
// SpaceComplexity : O(n)
21+
#include <iostream>
22+
using namespace std;
23+
24+
#pragma region DpArrayIdea
25+
namespace dp_array_idea {
26+
27+
class Solution {
28+
public:
29+
int climbStairs(int n) {
30+
int dp[45];
31+
32+
// dp[i]๋Š” i-1๋ฒˆ์งธ ์นธ์„ ์˜ค๋ฅด๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค.
33+
dp[0] = 1;
34+
dp[1] = 2;
35+
36+
for (int i = 2; i < n; i++) {
37+
dp[i] = dp[i-1] + dp[i-2];
38+
}
39+
40+
return dp[n-1];
41+
}
42+
};
43+
44+
} // namespace dp_array_idea
45+
#pragma endregion
46+
47+
// ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ’€์ด
48+
// Time : O(n)
49+
// Space : O(1)
50+
#pragma region FinalSolution
51+
class Solution {
52+
public:
53+
int climbStairs(int n) {
54+
if (n <= 2) {
55+
return n;
56+
}
57+
58+
int one_back = 1;
59+
int two_back = 2;
60+
61+
for (int i = 3; i <= n; i++) {
62+
int current = one_back + two_back;
63+
two_back = one_back;
64+
one_back = current;
65+
}
66+
67+
return one_back;
68+
}
69+
};
70+
#pragma endregion

โ€Žclimbing-stairs/Yu-Won.jsโ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* ๋ฌธ์ œ: https://leetcode.com/problems/climbing-stairs/description/
3+
*
4+
* ์š”๊ตฌ์‚ฌํ•ญ:
5+
* n๊ฐœ์˜ ๊ณ„๋‹จ์ด ์žˆ์„ ๋•Œ
6+
* ๊ณ„๋‹จ์€ 1, 2 ์นธ์”ฉ ์˜ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
7+
* ์ด ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ ์ด ๋ช‡๊ฐ€์ง€ ์ธ๊ฐ€?
8+
*
9+
* * */
10+
11+
const climbingStairs = (n) => {
12+
if(n <= 1) return 1;
13+
let prev1 = 1;
14+
let prev2 = 1;
15+
for(let i = 1; i < n; i++) {
16+
let current = prev1 + prev2;
17+
prev2 = prev1;
18+
prev1 = current;
19+
}
20+
21+
return prev1;
22+
}

0 commit comments

Comments
ย (0)