Skip to content

Commit 05bea89

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents fa8fa00 + 7ddce94 commit 05bea89

143 files changed

Lines changed: 4455 additions & 0 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/seueooo.jsβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
μ •λ ¬, νˆ¬ν¬μΈν„°
3+
μ •λ ¬ ν›„, 첫 번째 수λ₯Ό κ³ μ •ν•˜κ³  λ‚˜λ¨Έμ§€ 두 수λ₯Ό νˆ¬ν¬μΈν„°λ‘œ 탐색
4+
μ€‘λ³΅λ˜λŠ” κ²½μš°λŠ” μŠ€ν‚΅
5+
μ‹œκ°„ λ³΅μž‘λ„: O(n^2)
6+
곡간 λ³΅μž‘λ„: O(n)
7+
* @param {number[]} nums
8+
* @return {number[][]}
9+
*/
10+
var threeSum = function (nums) {
11+
nums.sort((a, b) => a - b);
12+
let n = nums.length;
13+
let answer = [];
14+
for (let i = 0; i < n - 2; i++) {
15+
let l = i + 1;
16+
let h = n - 1;
17+
if (nums[i] > 0) break;
18+
// 쀑볡 제거
19+
if (i > 0 && nums[i] === nums[i - 1]) continue;
20+
21+
while (l < h) {
22+
const sum = nums[i] + nums[l] + nums[h];
23+
if (sum > 0) {
24+
h--;
25+
} else if (sum < 0) {
26+
l++;
27+
} else {
28+
answer.push([nums[i], nums[l], nums[h]]);
29+
while (l < h && nums[l] === nums[l + 1]) l++;
30+
while (l < h && nums[h] === nums[h - 1]) h--;
31+
l++;
32+
h--;
33+
}
34+
}
35+
}
36+
return answer;
37+
};

β€Žclimbing-stairs/chapse57.pyβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def climbStairs(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
8+
9+
if n == 1:
10+
return 1
11+
if n == 2:
12+
return 2
13+
a = 1
14+
b = 2
15+
16+
for i in range(n - 2):
17+
c = a + b
18+
a = b
19+
b = c
20+
return c
21+

β€Žcoin-change/JeonJe.javaβ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
// TC: O(n * amount) (n = coins.length)
4+
// SC: O(amount)
5+
class Solution {
6+
7+
private static final int IMPOSSIBLE = Integer.MAX_VALUE;
8+
9+
private int[] memo;
10+
11+
public int coinChange(int[] coins, int amount) {
12+
memo = new int[amount + 1];
13+
Arrays.fill(memo, -1);
14+
15+
int result = minCoins(amount, coins);
16+
return result == IMPOSSIBLE ? -1 : result;
17+
}
18+
19+
private int minCoins(int target, int[] coins) {
20+
if (target == 0) return 0;
21+
if (target < 0) return IMPOSSIBLE;
22+
if (memo[target] != -1) return memo[target];
23+
24+
int best = IMPOSSIBLE;
25+
for (int coin : coins) {
26+
int sub = minCoins(target - coin, coins);
27+
if (sub != IMPOSSIBLE) {
28+
best = Math.min(best, sub + 1);
29+
}
30+
}
31+
return memo[target] = best;
32+
}
33+
}

β€Žcoin-change/alphaorderly.pyβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Time Complexity: O(n * amount)
3+
Space Complexity: O(n * amount)
4+
5+
- DP / Top-Down approach
6+
- Use a cache to store the results of the subproblems
7+
"""
8+
class Solution:
9+
def coinChange(self, coins: List[int], amount: int) -> int:
10+
LARGE = 10 ** 5
11+
12+
@cache
13+
def dp(coin: int, left: int) -> int:
14+
if left == 0:
15+
return 0
16+
17+
if left < 0 or coin >= len(coins):
18+
return LARGE
19+
20+
a = dp(coin, left - coins[coin]) + 1
21+
b = dp(coin + 1, left)
22+
23+
return min(a, b)
24+
25+
ans = dp(0, amount)
26+
27+
return ans if ans != LARGE else -1
28+
29+
"""
30+
Time Complexity: O(n * amount)
31+
Space Complexity: O(n * amount)
32+
33+
- DP / Bottom-Up approach
34+
- Use a 2D array to store the results of the subproblems
35+
"""
36+
class Solution:
37+
def coinChange(self, coins: List[int], amount: int) -> int:
38+
N = len(coins)
39+
dp = [[float('inf')] * (amount + 1) for _ in range(N)]
40+
41+
for c in range(N):
42+
dp[c][0] = 0
43+
44+
if c > 0:
45+
for a in range(amount + 1):
46+
dp[c][a] = dp[c - 1][a]
47+
48+
for a in range(coins[c], amount + 1):
49+
dp[c][a] = min(dp[c][a], dp[c][a - coins[c]] + 1)
50+
51+
ans = dp[-1][-1]
52+
53+
return ans if ans != float('inf') else -1
54+
55+
"""
56+
Time Complexity: O(n * amount)
57+
Space Complexity: O(amount)
58+
59+
- DP / Bottom-Up approach [Space Optimized]
60+
- Use a 1D array to store the results of the subproblems
61+
"""
62+
class Solution:
63+
def coinChange(self, coins: List[int], amount: int) -> int:
64+
N = len(coins)
65+
dp = [float("inf")] * (amount + 1)
66+
dp[0] = 0
67+
68+
for c in range(N):
69+
for a in range(coins[c], amount + 1):
70+
dp[a] = min(dp[a], dp[a - coins[c]] + 1)
71+
72+
return dp[-1] if dp[-1] != float("inf") else -1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* [풀이 κ°œμš”]
3+
* - μ‹œκ°„λ³΅μž‘λ„ : O(N * amount) (N: 동전 μ’…λ₯˜μ˜ 개수, μ΅œλŒ€ 12 * 10,000 = 120,000번 μ—°μ‚°)
4+
* - κ³΅κ°„λ³΅μž‘λ„ : O(amount) (DP ν…Œμ΄λΈ” 크기)
5+
*/
6+
class Solution {
7+
/**
8+
* [문제 풀이 아이디어]
9+
* - amountλ₯Ό κΈˆμ•‘μ΄λΌκ³  ν•΄μ„ν–ˆμ„ λ•Œ, κΈˆμ•‘μ„ λ§Œλ“€ 수 μžˆλŠ” μ΅œμ†Œ μ½”μΈμ˜ 갯수λ₯Ό κ΅¬ν•˜λŠ” 문제.
10+
* - κ°€μ§“ 수λ₯Ό μ˜λ―Έν•˜λŠ” 배열을 동해 동적 ν”„λ‘œκ·Έλž˜λ° λ°©μ‹μœΌλ‘œ ν’€ 수 있음.
11+
* - λ°”κΉ₯ λ£¨ν”„μ—μ„œλŠ” 코인을, μ•ˆμͺ½ λ£¨ν”„μ—μ„œλŠ” 수λ₯Ό μ°¨λ‘€λ‘œ μˆœνšŒν•¨.
12+
* - μ΄λ•Œ λͺ¨λ“  μˆ˜μ— λŒ€ν•΄ 각 μ½”μΈμœΌλ‘œ λ§Œλ“€ 수 μžˆλŠ” μ΅œμ € 수λ₯Ό κ³„μ‚°ν•΄μ„œ λ„£κ³ , μ½”μΈμ˜ μ•‘λ©΄ κ°’ 만큼 적은 νšŸμˆ˜μ—μ„œ ν•΄λ‹Ή 코인을 λ”ν•΄μ„œ λ§Œλ“€ 수 μžˆλŠ” μˆ˜μ™€ λΉ„κ΅ν•œ μ΅œμ†Ÿκ°’μ„ ꡬ함.
13+
* - μ‹œκ°„ λ³΅μž‘λ„λŠ” μ½”μΈμ˜ 수 * κΈˆμ•‘μ˜ 수 μ΄λ―€λ‘œ, 문제의 μ œμ•½μ— 따라 12 * 10,000 = 120000 이 λœλ‹€.
14+
*/
15+
public int coinChange(int[] coins, int amount) {
16+
int[] methods = new int[amount + 1]; // κΈˆμ•‘λ³„ μ½”μΈμ˜ μ΅œμ†Œ 갯수
17+
Arrays.fill(methods, amount+1);
18+
19+
methods[0] = 0; // κΈˆμ•‘ 0원을 λ§Œλ“€κΈ° μœ„ν•΄μ„œλŠ” 코인을 μ•ˆμ“°λ©΄ 됨.
20+
for(int i=0; i<coins.length; i++) {
21+
int coin = coins[i];
22+
for(int j=1; j <= amount; j++) {
23+
if(j >= coin) {
24+
methods[j] = Math.min(methods[j], methods[j - coin] + 1);
25+
}
26+
}
27+
}
28+
29+
return methods[amount] == amount + 1 ? -1 : methods[amount];
30+
}
31+
}

β€Žcoin-change/dolphinflow86.pyβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 1) Use top-down memoization, recursively calculate the minimum coins required for each remaining amount and cache the results to prune duplicated branches.
2+
# TC: O(K^M) where K is the number of coins, where M is the amount. We can recurse down at most M depth
3+
# SC: O(K + M)
4+
class Solution:
5+
def dfs(self, coins: List[int], memo:Dict[int, int], remain: int):
6+
if remain == 0: return 0
7+
if remain < 0: return float('inf')
8+
if remain in memo: return memo[remain]
9+
10+
min_result = float('inf')
11+
for coin in coins:
12+
min_result = min(min_result, self.dfs(coins, memo, remain - coin))
13+
14+
memo[remain] = min_result if min_result == float('inf') else min_result + 1
15+
return memo[remain]
16+
17+
def coinChange(self, coins: List[int], amount: int) -> int:
18+
result = self.dfs(coins, {}, amount)
19+
return result if result != float('inf') else -1

β€Žcoin-change/okyungjin.pyβ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# coins: λ™μ „μ˜ 가격이 적힌 μ •μˆ˜ λ°°μ—΄, 각 μˆ«μžλŠ” μœ λ‹ˆν¬ν•¨, 정렬에 λŒ€ν•œ μ–ΈκΈ‰ X, (1 <= coins.length <= 12)
2+
# amount: 돈의 총합
3+
4+
# amountλ₯Ό λ§Œλ“€ 수 μžˆλŠ” κ°€μž₯ 적은 λ™μ „μ˜ 수λ₯Ό λ°˜ν™˜ν•œλ‹€.
5+
# coins둜 amountλ₯Ό λ§Œλ“€ 수 μ—†λŠ” 경우 -1을 λ°˜ν™˜
6+
# λ™μ „μ˜ μ’…λ₯˜λŠ” λ¬΄ν•œλŒ€λΌκ³  κ°€μ •ν•˜κ³  문제λ₯Ό ν’€ 것
7+
8+
# μ²˜μŒμ—λŠ” greedy둜 μ ‘κ·Όν•˜λ €κ³  ν–ˆλŠ”λ°, κΈˆμ•‘μ΄ 큰 동전을 λ¨Όμ € μ„ νƒν•˜λŠ”κ²Œ μ΅œμ†Œ λ™μ „μ˜ μˆ˜κ°€ μ•„λ‹ˆλΌμ„œ dp둜 ν’€μ΄ν–ˆλ‹€.
9+
10+
# [λ³΅μž‘λ„]
11+
# C: μ½”μΈμ˜ 개수, # A: amount
12+
# μ‹œκ°„ λ³΅μž‘λ„: O(A * C)
13+
# 곡간 λ³΅μž‘λ„: O(A)
14+
class Solution:
15+
def coinChange(self, coins: List[int], amount: int) -> int:
16+
NOT_POSSIBLE = amount + 1
17+
18+
# min_coins: κΈˆμ•‘μ„ λ§Œλ“œλŠ”λ° ν•„μš”ν•œ 동전 개수λ₯Ό μ €μž₯ν•˜λŠ” λ°°μ—΄
19+
# min_coins[i]: i원을 λ§Œλ“œλŠ”λ° ν•„μš”ν•œ μ΅œμ†Œ λ™μ „μ˜ 개수
20+
min_coins = [NOT_POSSIBLE] * (amount + 1)
21+
min_coins[0] = 0
22+
23+
# μ΅œμ ν™”λ₯Ό μœ„ν•΄ μ •λ ¬
24+
coins.sort()
25+
26+
for cur_amount in range(1, amount + 1):
27+
# coin: μ„ νƒν•œ λ™μ „μ˜ κΈˆμ•‘
28+
for coin in coins:
29+
# coinsκ°€ μ •λ ¬λ˜μ–΄ μžˆμœΌλ―€λ‘œ, cur_amount 보닀 λ™μ „μ˜ κΈˆμ•‘μ΄ 더 크면 μ΄ν›„λŠ” 확인 λΆˆν•„μš”
30+
if coin > cur_amount:
31+
break
32+
33+
# λ‘˜ 쀑 더 μž‘μ€ κ°’μœΌλ‘œ μ—…λ°μ΄νŠΈ
34+
# 1. 기쑴에 κ΅¬ν•œ 개수
35+
# 2. ν˜„μž¬ 동전을 1개 μΆ”κ°€ν•΄μ„œ λ§Œλ“œλŠ” 개수
36+
min_coins[cur_amount] = min(min_coins[cur_amount], min_coins[cur_amount - coin] + 1)
37+
38+
# μ΄ˆκΈ°ν™”λœ κ°’ κ·ΈλŒ€λ‘œμ΄λ©΄ λΆˆκ°€λŠ₯ν•œ μ‘°ν•©μ΄λ―€λ‘œ -1 λ°˜ν™˜
39+
if min_coins[amount] == NOT_POSSIBLE:
40+
return -1
41+
# μ΅œμ†Œ λ™μ „μ˜ 개수λ₯Ό λ°˜ν™˜
42+
else:
43+
return min_coins[amount]

β€Žcoin-change/yuseok89.pyβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TC: O(amount * len(coins))
2+
# SC: O(amount)
3+
class Solution:
4+
def coinChange(self, coins: List[int], amount: int) -> int:
5+
6+
from collections import deque
7+
8+
v = {0: 0}
9+
q = deque([0])
10+
11+
while q and amount not in v:
12+
13+
cur = q.popleft()
14+
15+
for coin in coins:
16+
17+
if cur + coin > amount or cur + coin in v:
18+
continue
19+
20+
v[cur + coin] = v[cur] + 1
21+
q.append(cur + coin)
22+
23+
return -1 if amount not in v else v[amount]
24+

β€Žcombination-sum/Chanz82.pyβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
4+
result = []
5+
combination = []
6+
def find_target(start, combination, target):
7+
8+
if target == 0 : # μ‘°ν•© 생성 μ™„λ£Œ.
9+
result.append(combination.copy()) # pop λ•Œλ¬Έμ— λ³΅μ‚¬ν•΄μ„œ appendν•΄μ•Ό 함.
10+
return
11+
12+
elif target < 0 or start >= len(candidates):
13+
return
14+
15+
combination.append(candidates[start])
16+
find_target(start, combination, target-candidates[start]) # ν˜„μž¬ μ›μ†Œλ₯Ό 더 λ½‘λŠ” 경우
17+
combination.pop() # ν˜„μž¬ μ›μ†ŒλŠ” λ‹€μ‹œ μ‘°ν•©μ—μ„œ 제거 ν›„, λ‹€μŒ μ›μ†Œλ₯Ό λ½‘μŒ
18+
find_target(start+1, combination, target) # λ‹€μŒ μ›μ†Œλ₯Ό λ½‘λŠ” 경우
19+
20+
find_target(0, combination, target)
21+
22+
return result

β€Žcombination-sum/ICE0208.javaβ€Ž

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
/*
6+
* [λ°±νŠΈλž˜ν‚Ή 풀이]
7+
* N: candidates의 길이
8+
* M: candidatesμ—μ„œ κ°€μž₯ μž‘μ€ κ°’
9+
*
10+
* μ‹œκ°„ λ³΅μž‘λ„: O(N^(target / M))
11+
* - μž¬κ·€ ν•œ λ‹¨κ³„μ—μ„œ μ΅œλŒ€ N개의 후보λ₯Ό 선택할 수 μžˆλ‹€.
12+
* - κ°€μž₯ μž‘μ€ 숫자λ₯Ό λ°˜λ³΅ν•΄μ„œ μ„ νƒν•˜λ©΄ μž¬κ·€ κΉŠμ΄λŠ” μ΅œλŒ€ target / M이닀.
13+
* - μ‹€μ œλ‘œλŠ” startIndex와 κ°€μ§€μΉ˜κΈ°λ‘œ 인해 이보닀 적은 경우만 νƒμƒ‰ν•œλ‹€.
14+
*
15+
* 곡간 λ³΅μž‘λ„: O(target / M)
16+
* - μž¬κ·€ 호좜 μŠ€νƒκ³Ό ν˜„μž¬ μ‘°ν•© λ¦¬μŠ€νŠΈκ°€ μ΅œλŒ€ μž¬κ·€ 깊이만큼 μ‚¬μš©λœλ‹€.
17+
* - λ°˜ν™˜ν•  κ²°κ³Ό λ¦¬μŠ€νŠΈλŠ” μ œμ™Έν•œλ‹€.
18+
*/
19+
20+
private int[] candidates;
21+
private List<List<Integer>> combinations;
22+
23+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
24+
this.candidates = candidates;
25+
this.combinations = new ArrayList<>();
26+
27+
findCombinations(0, target, new ArrayList<>());
28+
29+
return combinations;
30+
}
31+
32+
private void findCombinations(
33+
int startIndex,
34+
int remainingTarget,
35+
List<Integer> currentCombination
36+
) {
37+
// λͺ©ν‘œ 합을 μ •ν™•νžˆ μ™„μ„±ν•œ 경우
38+
if (remainingTarget == 0) {
39+
combinations.add(new ArrayList<>(currentCombination));
40+
return;
41+
}
42+
43+
for (int i = startIndex; i < candidates.length; i++) {
44+
int candidate = candidates[i];
45+
46+
// ν˜„μž¬ 숫자λ₯Ό μ„ νƒν•˜λ©΄ λͺ©ν‘œ 합을 μ΄ˆκ³Όν•˜λŠ” 경우
47+
if (candidate > remainingTarget) {
48+
continue;
49+
}
50+
51+
currentCombination.add(candidate);
52+
53+
// 같은 숫자λ₯Ό μ—¬λŸ¬ 번 μ‚¬μš©ν•  수 μžˆμœΌλ―€λ‘œ iλΆ€ν„° λ‹€μ‹œ νƒμƒ‰ν•œλ‹€.
54+
findCombinations(
55+
i,
56+
remainingTarget - candidate,
57+
currentCombination
58+
);
59+
60+
// λ‹€μŒ 쑰합을 νƒμƒ‰ν•˜κΈ° μœ„ν•΄ ν˜„μž¬ 선택을 λ˜λŒλ¦°λ‹€.
61+
currentCombination.remove(currentCombination.size() - 1);
62+
}
63+
}
64+
}

0 commit comments

Comments
Β (0)