Skip to content

Commit 0424c1f

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 5190fb0 + 3d0fa90 commit 0424c1f

File tree

106 files changed

+4007
-0
lines changed

Some content is hidden

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

106 files changed

+4007
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Runtime: 1ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 94.08MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: 그리디 알고리즘
9+
* - 주어진 prices 배열을 순회하며 최소 가격(min)을 갱신
10+
*/
11+
class Solution {
12+
public int maxProfit(int[] prices) {
13+
int maxProfit = 0;
14+
int min = prices[0];
15+
16+
for (int i=1; i<prices.length; i++) {
17+
if (min >= prices[i]) {
18+
min = prices[i];
19+
} else {
20+
maxProfit = Math.max(maxProfit, prices[i]-min);
21+
}
22+
}
23+
24+
return maxProfit;
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
// 주식 최대 이익
4+
int minPrice = Integer.MAX_VALUE;
5+
int maxProfit = 0;
6+
7+
for (int price : prices) {
8+
minPrice = Math.min(minPrice, price);
9+
maxProfit = Math.max(maxProfit, price - minPrice);
10+
}
11+
12+
return maxProfit;
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
# profit: 최대 이익
4+
profit = 0
5+
6+
# current_minimum: 현재까지의 최소 가격
7+
current_minimum = prices[0]
8+
9+
# current_profit: 현재까지의 최대 이익
10+
current_profit = 0
11+
12+
# 전체 배열에서 최솟값과, 최솟값 인덱스 이후 최댓값을 이익으로 계산하는 반복문
13+
for i in range(1, len(prices)):
14+
# 이익이 발생할 경우 현재 이익 갱신
15+
# 현재 이익을 갱신할 때마다 지금까지의 최대 이익도 갱신
16+
if prices[i] > current_minimum:
17+
current_profit = max(current_profit, prices[i] - current_minimum)
18+
profit = max(profit, current_profit)
19+
# 이익이 발생하지 않을 경우 최솟값 갱신
20+
else:
21+
current_minimum = prices[i]
22+
23+
# 최대 이익만 저장되어 반환
24+
return profit

coin-change/Blossssom.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param coins - 동전 종류 배열
3+
* @param amount - 총 금액
4+
* @returns - 총 사용 코인 갯수
5+
* @description
6+
* - 탐욕으로 풀면 최소 동전갯수를 구할 수 없음
7+
* - dp 로 풀이
8+
* - 1 부터 금액의 코인 사용 갯수를 추가해 가며, 이전에 구해놓은 값과 min 비교
9+
*/
10+
function coinChange(coins: number[], amount: number): number {
11+
const dp = new Array(amount + 1).fill(Infinity);
12+
13+
dp[0] = 0;
14+
15+
for (let i = 1; i <= amount; i++) {
16+
for (const coin of coins) {
17+
if (i - coin >= 0) {
18+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
19+
}
20+
}
21+
}
22+
23+
return dp[amount] === Infinity ? -1 : dp[amount];
24+
}
25+
26+
const coins = [1, 2, 5];
27+
const amount = 11;
28+
coinChange(coins, amount);
29+

coin-change/ZetBe.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
문제: 주어진 동전 종류로 특정 금액을 만들기 위한 최소 동전 개수를 구하시오.
3+
풀이: 동적 계획법(DP)을 사용하여 각 금액에 대해 최소 동전 개수를 계산합니다. 만약 특정 금액을 만들 수 없다면 -1을 반환합니다.
4+
시간 복잡도: O(n * m), n은 금액(amount), m은 동전 종류의 개수입니다. 각 금액에 대해 모든 동전을 확인하므로 전체 시간 복잡도는 O(n * m)입니다.
5+
공간 복잡도: O(n), 금액(amount)까지의 최소 동전 개수를 저장하는 DP 배열을 사용하므로 공간 복잡도는 O(n)입니다.
6+
사용한 자료구조: 배열(DP 배열)
7+
'''
8+
9+
10+
class Solution:
11+
def coinChange(self, coins: List[int], amount: int) -> int:
12+
if amount == 0:
13+
return 0
14+
if len(coins) == 1 and coins[0] > amount:
15+
return -1
16+
if amount in coins:
17+
return 1
18+
19+
dp = [0 for i in range(amount+1)]
20+
21+
for i in range(amount+1):
22+
for j in range(len(coins)):
23+
if i == 0 and coins[j] < amount:
24+
dp[coins[j]] = 1
25+
elif i > 0 and 0 <= i+coins[j] <= amount:
26+
if dp[i] > 0 and dp[i+coins[j]] > 0:
27+
dp[i+coins[j]] = min(dp[i+coins[j]], dp[i]+1)
28+
elif dp[i] > 0 and dp[i+coins[j]] == 0:
29+
dp[i+coins[j]] = dp[i]+1
30+
31+
32+
33+
34+
if dp[amount] == 0:
35+
return -1
36+
return dp[amount]
37+
38+

coin-change/casentino.ts

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

coin-change/daiyongg-kim.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
if amount == 0:
4+
return 0
5+
6+
level = [0]
7+
visited = {0}
8+
count = 0
9+
10+
while level:
11+
count += 1
12+
next_level = []
13+
14+
for current_amount in level:
15+
for coin in coins:
16+
new_amount = current_amount + coin
17+
18+
if new_amount == amount:
19+
return count
20+
21+
if new_amount < amount and new_amount not in visited:
22+
visited.add(new_amount)
23+
next_level.append(new_amount)
24+
level = next_level
25+
26+
return -1

coin-change/dylan-jung.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
long dp[10001];
4+
int coinChange(vector<int>& coins, int amount) {
5+
long INF = (long)1 << 31;
6+
fill(dp, dp+10001, INF);
7+
8+
for(int& item: coins) {
9+
if(item > 10000) continue;
10+
dp[item] = 1;
11+
}
12+
13+
for(int i = 1; i <= amount; i++) {
14+
for(int& item: coins) {
15+
int j = i - item;
16+
if(j > 0 && dp[j] != -1) {
17+
dp[i] = min(dp[j] + 1, dp[i]);
18+
}
19+
}
20+
}
21+
22+
if(dp[amount] == INF) {
23+
if(amount == 0) return 0;
24+
else return -1;
25+
}
26+
return dp[amount];
27+
}
28+
};

coin-change/kimjunyoung90.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
//풀이 원리 = 특정 금액을 만들 때 최소 동전 수는 몇 개?
5+
//예) 0원은 동전 0개
6+
// 1원은 동전 1개
7+
// 2원은 동전 1
8+
public int coinChange(int[] coins, int amount) {
9+
//불가능한 값
10+
int max = amount + 1;
11+
int[] dp = new int[amount + 1];
12+
//최소 동전 수를 모두 불가능한 값으로 설정
13+
Arrays.fill(dp, max);
14+
dp[0] = 0;
15+
16+
//금액 1원 부터 계산
17+
for (int i = 1; i <= amount; i++) {
18+
//사용할 수 있는 동전을 꺼냄
19+
for(int coin: coins) {
20+
if (coin <= i) {
21+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
22+
}
23+
}
24+
}
25+
26+
return dp[amount] > amount ? -1 : dp[amount];
27+
}
28+
}

coin-change/ppxyn1.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# idea: DFS/BFS, DP
2+
3+
from collections import deque
4+
class Solution:
5+
def coinChange(self, coins: List[int], amount: int) -> int:
6+
# Why Greedy is not possiple way?
7+
# A greedy is only optimal in specific coin systems (e.g., denominations like 1, 5, 10, 25)
8+
# For arbitrary coin denominations, a greedy approach does not always yield the optimal solution.
9+
queue = deque([(0,0)]) # (동전갯수, 누적금액)
10+
while queue:
11+
count, total = queue.popleft()
12+
if total == amount:
13+
return count
14+
for coin in coins:
15+
if total + coin <= amount:
16+
queue.append([count+1, total+ coin])
17+
return -1
18+
19+
# # BFS
20+
# def coinChange(self, coins: List[int], amount: int) -> int:
21+
# queue = deque([(0,0)]) # (동전갯수, 누적금액)
22+
# visited = set()
23+
# while queue:
24+
# count, total = queue.popleft()
25+
# if total == amount:
26+
# return count
27+
# if total in visited:
28+
# continue
29+
# visited.add(total)
30+
# for coin in coins:
31+
# if total + coin <= amount:
32+
# queue.append([count+1, total+ coin])
33+
# return -1
34+
35+
36+
# DP
37+
# dp[i] = min(dp[i], dp[i-coin]+1)
38+
# from collections import deque
39+
# class Solution:
40+
# def coinChange(self, coins: List[int], amount: int) -> int:
41+
# dp=[0]+[amount+1]*amount
42+
# for coin in coins:
43+
# for i in range(coin, amount+1):
44+
# dp[i] = min(dp[i], dp[i-coin]+1)
45+
# return dp[amount] if dp[amount] < amount else -1
46+
47+

0 commit comments

Comments
 (0)