Skip to content

Commit c0c19ab

Browse files
author
sangbeenmoon
committed
2 parents 912181e + 8a53dae commit c0c19ab

156 files changed

Lines changed: 3849 additions & 7 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/DaleSeo.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TC: O(n^2)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
5+
nums.sort_unstable();
6+
let n = nums.len();
7+
let mut triplets = Vec::new();
8+
9+
for i in 0..n {
10+
if i > 0 && nums[i] == nums[i - 1] {
11+
continue;
12+
}
13+
14+
let (mut low, mut high) = (i + 1, n - 1);
15+
while low < high {
16+
let three_sum = nums[i] + nums[low] + nums[high];
17+
if three_sum < 0 {
18+
low += 1;
19+
} else if three_sum > 0 {
20+
high -= 1;
21+
} else {
22+
triplets.push(vec![nums[i], nums[low], nums[high]]);
23+
low += 1;
24+
high -= 1;
25+
while low < high && nums[low] == nums[low - 1] {
26+
low += 1;
27+
}
28+
while low < high && nums[high] == nums[high + 1] {
29+
high -= 1;
30+
}
31+
}
32+
}
33+
}
34+
35+
triplets
36+
}
37+
}

3sum/alphaorderly.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def threeSum(self, nums: list[int]) -> list[list[int]]:
3+
"""
4+
O(N^2)
5+
"""
6+
cntr = Counter(nums)
7+
8+
ans = []
9+
10+
# First case -> [0, 0, 0] Triplet
11+
if cntr[0] >= 3:
12+
ans.append([0, 0, 0])
13+
14+
# Second case -> two same numbers + one diff number
15+
for k, v in cntr.items():
16+
if k == 0:
17+
continue
18+
if v >= 2 and -(k * 2) in cntr:
19+
ans.append([k, k, -(k * 2)])
20+
21+
s = set(nums)
22+
nums = list(s)
23+
nums.sort()
24+
25+
pos = {k: v for v, k in enumerate(nums)}
26+
N = len(nums)
27+
28+
# Third case -> all different numbers
29+
for i in range(N - 2):
30+
for j in range(i + 1, N - 1):
31+
target = -(nums[i] + nums[j])
32+
if target in pos:
33+
k = pos[target]
34+
if k > j:
35+
ans.append([nums[i], nums[j], nums[k]])
36+
37+
return ans

3sum/dolphinflow86.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 1) Fix one element first, and then treat others as two-sum problem. Use set to get rid of duplicate combination.
2+
# TC: O(N^2) where N is the length of nums.
3+
# SC: O(N) where N is the length of nums.
4+
class Solution:
5+
def threeSum(self, nums: list[int]) -> list[list[int]]:
6+
answer: Set[tuple[int,int,int]] = set()
7+
n = len(nums)
8+
nums.sort()
9+
10+
for i in range(n-2):
11+
if i > 0 and nums[i] == nums[i-1]: continue
12+
13+
target = -nums[i]
14+
nums_map: Dict[int, int] = {}
15+
for j in range(i + 1, n):
16+
comp = target - nums[j]
17+
if comp in nums_map:
18+
answer.add((nums[i], comp, nums[j]))
19+
nums_map[nums[j]] = j
20+
21+
return [list(triplet) for triplet in answer]

3sum/jaekwang97.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> threeSum(int[] nums) {
5+
int n = nums.length;
6+
Arrays.sort(nums);
7+
List<List<Integer>> answers = new ArrayList<>();
8+
Set<List<Integer>> answer = new HashSet<>();
9+
for(int i = 0 ; i < n ; i++){
10+
if (nums[i] > 0) break;
11+
if (i > 0 && nums[i] == nums[i-1]) continue;
12+
int left = i + 1;
13+
int right = n - 1;
14+
while(left < right){
15+
int sum = nums[left] + nums[right] + nums[i];
16+
17+
18+
if (sum > 0) right--;
19+
else if (sum < 0) left++;
20+
else{
21+
answer.add(new ArrayList<>(List.of(nums[i],nums[left], nums[right])));
22+
while(left < right && nums[left] == nums[left+1]) left++;
23+
while(left < right && nums[right] == nums[right-1]) right--;
24+
left++;
25+
right--;
26+
}
27+
}
28+
}
29+
30+
answers = new ArrayList<>(answer);
31+
32+
return answers;
33+
}
34+
}

3sum/yuseok89.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# TC: O(N^2)
2+
# SC: O(N)
3+
class Solution:
4+
def threeSum(self, nums: list[int]) -> list[list[int]]:
5+
6+
cnt_dict = Counter(nums)
7+
nums_uniq = sorted(cnt_dict.keys())
8+
n = len(nums_uniq)
9+
10+
ret = []
11+
12+
for num in cnt_dict.keys():
13+
if num == 0:
14+
if cnt_dict[num] >= 3:
15+
ret.append([0, 0, 0])
16+
elif cnt_dict[num] >= 2 and num * 2 * -1 in cnt_dict:
17+
ret.append([num, num, num * 2 * -1])
18+
19+
for idx1, num1 in enumerate(nums_uniq):
20+
if num1 > 0:
21+
break
22+
23+
for idx2 in range(idx1 + 1, n):
24+
num2 = nums_uniq[idx2]
25+
num3 = (num1 + num2) * -1
26+
27+
if num2 >= num3:
28+
break
29+
elif num3 in cnt_dict:
30+
ret.append([num1, num2, num3])
31+
32+
return ret
33+

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
## 차수
1212

13-
- 7기 (Mar 1, 2025 - Jun 13, 2026): [프로젝트](https://github.com/orgs/DaleStudy/projects/26/views/3), [](https://github.com/orgs/DaleStudy/teams/leetcode07)
13+
- 8기 (Jun 21, 2026 - Oct 3, 2026): [프로젝트](https://github.com/orgs/DaleStudy/projects/29/views/2), [](https://github.com/orgs/DaleStudy/teams/leetcode08)
14+
- 7기 (Mar 1, 2026 - Jun 13, 2026): [프로젝트](https://github.com/orgs/DaleStudy/projects/26/views/3), [](https://github.com/orgs/DaleStudy/teams/leetcode07)
1415
- 6기 (Nov 8, 2025 - Feb 20, 2026): [프로젝트](https://github.com/orgs/DaleStudy/projects/23/views/3), [](https://github.com/orgs/DaleStudy/teams/leetcode06)
1516
- 5기 (Jul 20, 2025 - Nov 1, 2025): [프로젝트](https://github.com/orgs/DaleStudy/projects/16/views/3), [](https://github.com/orgs/DaleStudy/teams/leetcode05)
1617
- 4기 (Mar 30, 2025 - Jul 12, 2025): [프로젝트](https://github.com/orgs/DaleStudy/projects/13/views/3), [](https://github.com/orgs/DaleStudy/teams/leetcode04)

climbing-stairs/alphaorderly.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# class Solution:
2+
# def climbStairs(self, n: int) -> int:
3+
# """
4+
# O(N) with additional space
5+
# """
6+
# if n <= 2:
7+
# return n
8+
9+
# dp = [0] * (n + 1)
10+
# dp[1] = 1
11+
# dp[2] = 2
12+
13+
# for i in range(3, n + 1):
14+
# dp[i] = dp[i - 1] + dp[i - 2]
15+
16+
# return dp[-1]
17+
18+
# class Solution:
19+
# """
20+
# O(N) Found that solution is fibonacci number
21+
# """
22+
# def climbStairs(self, n: int) -> int:
23+
# if n <= 2:
24+
# return n
25+
26+
# p1, p2 = 1, 2
27+
# for i in range(3, n + 1):
28+
# p1, p2 = p2, p1 + p2
29+
30+
# return p2
31+
32+
33+
# Fibonacci hack
34+
class Solution:
35+
def climbStairs(self, n: int) -> int:
36+
"""
37+
O(Log N)
38+
"""
39+
40+
# O(1) operation
41+
def mat_mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
42+
ans = [[0] * 2 for _ in range(2)]
43+
44+
for i in range(2):
45+
for j in range(2):
46+
for k in range(2):
47+
ans[i][j] += a[i][k] * b[k][j]
48+
49+
return ans
50+
51+
# O(Log N) operation
52+
def mat_pow(a: List[List[int]], n: int) -> List[List[int]]:
53+
if n == 1:
54+
return [[1, 1], [1, 0]]
55+
56+
half = mat_pow(a, n // 2)
57+
multed = mat_mul(half, half)
58+
59+
if n % 2:
60+
return mat_mul(multed, [[1, 1], [1, 0]])
61+
else:
62+
return multed
63+
64+
ans = mat_pow([[1, 1], [1, 0]], n)
65+
66+
return ans[0][0]

climbing-stairs/dolphinflow86.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 1) Recursion with memoization.
2+
# TC: O(N) where N is the given natural number.
3+
# SC: O(N) where N is the given natural number.
4+
class Solution:
5+
def climb_rec(self, n: int, step: int, memo: Dict[int, int]):
6+
if step > n: return 0
7+
if step in memo: return memo[step]
8+
if step == n: return 1
9+
10+
first_way = self.climb_rec(n, step + 1, memo)
11+
second_way = self.climb_rec(n, step + 2, memo)
12+
memo[step] = first_way + second_way
13+
return memo[step]
14+
15+
16+
def climbStairs(self, n: int) -> int:
17+
if n == 1: return 1
18+
19+
memo = {}
20+
return self.climb_rec(n, 1, memo) + self.climb_rec(n, 2, memo)

climbing-stairs/jaekwang97.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
if (n <= 2) return n;
4+
5+
int prev = 1;
6+
int cur = 2;
7+
8+
for (int i = 3; i <= n; i++) {
9+
int next = prev + cur;
10+
prev = cur;
11+
cur = next;
12+
}
13+
14+
return cur;
15+
}
16+
}

climbing-stairs/yuseok89.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TC: O(N)
2+
# SC: O(1)
3+
class Solution:
4+
def climbStairs(self, n: int) -> int:
5+
6+
one_down = 1
7+
two_down = 0
8+
9+
for _ in range(1, n):
10+
cur = two_down + one_down
11+
two_down = one_down
12+
one_down = cur
13+
14+
return two_down + one_down
15+

0 commit comments

Comments
 (0)