Skip to content

Commit 952055a

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 7963135 + 8c34ca2 commit 952055a

94 files changed

Lines changed: 3247 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.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
5+
- Single Pass approach
6+
- Use a variable to store the least price
7+
- Use a variable to store the maximum profit
8+
- Update the maximum profit whenever the current price is greater than the least price
9+
"""
10+
class Solution:
11+
def maxProfit(self, prices: List[int]) -> int:
12+
N = len(prices)
13+
14+
least = prices[0]
15+
ans = 0
16+
17+
for i in range(1, N):
18+
ans = max(ans, prices[i] - least)
19+
least = min(least, prices[i])
20+
21+
return ans

coin-change/ICE0208.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
/**
3+
* 시간 복잡도: O(amount * coins.length)
4+
* 공간 복잡도: O(amount)
5+
*/
6+
public int coinChange(int[] coins, int amount) {
7+
// 필요한 동전의 최대 개수는 amount개이므로,
8+
// amount + 1은 만들 수 없는 상태를 나타내기에 충분하다.
9+
int impossible = amount + 1;
10+
11+
int[] dp = new int[amount + 1];
12+
Arrays.fill(dp, impossible);
13+
dp[0] = 0;
14+
15+
for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {
16+
for (int coin : coins) {
17+
if (coin > currentAmount) {
18+
continue;
19+
}
20+
21+
dp[currentAmount] = Math.min(
22+
dp[currentAmount],
23+
dp[currentAmount - coin] + 1
24+
);
25+
}
26+
}
27+
28+
return dp[amount] == impossible ? -1 : dp[amount];
29+
}
30+
}

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/Yiseull.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
3+
private int answer = Integer.MAX_VALUE;
4+
5+
public int coinChange(int[] coins, int amount) {
6+
final int IMPOSSIBLE = amount + 1;
7+
8+
int[] dp = new int[amount + 1];
9+
Arrays.fill(dp, IMPOSSIBLE);
10+
dp[0] = 0;
11+
12+
for (int i = 1; i < amount + 1; i++) {
13+
for (int coin : coins) {
14+
if (coin <= i) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
16+
}
17+
}
18+
}
19+
20+
return dp[amount] == IMPOSSIBLE ? -1 : dp[amount];
21+
}
22+
}

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

coin-change/dahyeong-yun.java

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/freemjstudio.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
if amount == 0:
4+
return 0
5+
INF = int(1e9)
6+
# dp[n] 은 n 원을 만들기 위한 최소한의 동전 개수를 저장한다.
7+
dp = [INF] * (amount + 1)
8+
usable_coins = []
9+
for coin in coins:
10+
if coin == amount:
11+
return 1
12+
if coin < amount:
13+
dp[coin] = 1 # dp[3] = 1
14+
usable_coins.append(coin)
15+
16+
# dp 를 채워나가는 과정
17+
for i in range(1, amount+1):
18+
for coin in usable_coins:
19+
if (i - coin) >= 0 and dp[i - coin] != INF:
20+
dp[i] = min(dp[i - coin] + 1, dp[i])
21+
22+
return -1 if dp[amount] == INF else dp[amount]

coin-change/j2h30728.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
const dp = new Array(amount + 1).fill(amount + 1);
3+
dp[0] = 0;
4+
5+
for (let i = 1; i <= amount; i++) {
6+
for (const coin of coins) {
7+
if (i - coin >= 0) {
8+
dp[i] = Math.min(dp[i], 1 + dp[i - coin]);
9+
}
10+
}
11+
}
12+
13+
return dp[amount] > amount ? -1 : dp[amount];
14+
};

coin-change/jaekwang97.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int coinChange(int[] coins, int amount) {
5+
int[] dp = new int[amount + 1];
6+
7+
Arrays.fill(dp, amount + 1);
8+
dp[0] = 0;
9+
10+
for (int i = 1; i <= amount; i++) {
11+
for (int coin : coins) {
12+
if (i - coin >= 0) {
13+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
14+
}
15+
}
16+
}
17+
18+
return dp[amount] == amount + 1 ? -1 : dp[amount];
19+
}
20+
}

0 commit comments

Comments
 (0)