Skip to content

Commit 5c62512

Browse files
authored
Merge branch 'AlgorithmStudy-Allumbus:main' into main
2 parents 39c7473 + e29faa4 commit 5c62512

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
BOJ #17845. ์ˆ˜๊ฐ•๊ณผ๋ชฉ (๊ณจ๋“œ5)
3+
https://www.acmicpc.net/problem/17845
4+
์œ ํ˜•: DP, ๋ฐฐ๋‚ญ๋ฌธ์ œ
5+
'''
6+
import sys
7+
input = sys.stdin.readline
8+
9+
n, k = list(map(int, input().rstrip().split())) # n์€ ์ตœ๋Œ€ ๊ณต๋ถ€ ์‹œ๊ฐ„, k๋Š” ๊ณผ๋ชฉ ์ˆ˜
10+
info = [0] + [list(map(int, input().rstrip().split())) for _ in range(k)]
11+
dp = [[0] * (n + 1) for _ in range(k + 1)]
12+
13+
for i in range(1, k + 1): # i๋Š” ํ˜„์žฌ ๊ณ ๋ฅธ ๊ณผ๋ชฉ ์ˆ˜
14+
for j in range(1, n + 1): # j๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„
15+
if j < info[i][1]: # ์‹œ๊ฐ„์ด ๋„˜์นœ๋‹ค๋ฉด
16+
dp[i][j] = dp[i - 1][j] # ํ•ด๋‹น ๊ณผ๋ชฉ์€ ์ œ์™ธ
17+
else: # ์‹œ๊ฐ„์ด ๋„˜์น˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
18+
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - info[i][1]] + info[i][0]) # ํ•ด๋‹น ๊ณผ๋ชฉ ์ œ์™ธํ•œ๊ฑฐ vs ํ•ด๋‹น ๊ณผ๋ชฉ ์ค‘์š”๋„ + (ํ•ด๋‹น ๊ณผ๋ชฉ ์ œ์™ธ) ํ•ด๋‹น ๊ณผ๋ชฉ์˜ ์‹œ๊ฐ„์„ ๋บ€ ์ค‘์š”๋„
19+
20+
print(dp[k][n])
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## ๐Ÿš€ 11์›” 4์ฃผ์ฐจ (11/25) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: Dynamic Programming
2+
> ๋ฐœ์ œ์ž: ๊น€๋ฏผ์ • (@Mingguriguri)
3+
4+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
5+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_1125.pdf)
6+
<img width="800" alt="แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-11-25 แ„‹แ…ฉแ„’แ…ฎ 9 21 34" src="https://github.com/user-attachments/assets/5e0d93dd-4f45-4405-b0e1-ab9383c36bc1">
7+
8+
### ๐Ÿ“– ๋ฌธ์ œ
9+
- [๋ฐฑ์ค€ #12865. ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ](https://www.acmicpc.net/problem/12865): DP, ๋ฐฐ๋‚ญ๋ฌธ์ œ / ๊ณจ๋“œ5
10+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_12865_ํ‰๋ฒ”ํ•œ๋ฐฐ๋‚ญ.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_12865_%ED%8F%89%EB%B2%94%ED%95%9C%EB%B0%B0%EB%82%AD.py#L1-L30)
11+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/bc8ac11b8f5d9d9e8e57baaef984b192b2b03312/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_12865_%ED%8F%89%EB%B2%94%ED%95%9C%EB%B0%B0%EB%82%AD.py#L1-L30
12+
13+
### ๐Ÿ’ป ๊ณผ์ œ
14+
- [๋ฐฑ์ค€ #17845. ์ˆ˜๊ฐ•๊ณผ๋ชฉ](https://www.acmicpc.net/problem/17845): DP, ๋ฐฐ๋‚ญ๋ฌธ์ œ/ ๊ณจ๋“œ5
15+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_17845_์ˆ˜๊ฐ•๊ณผ๋ชฉ.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Assignment_BOJ_17845_%EC%88%98%EA%B0%95%EA%B3%BC%EB%AA%A9.py#L1-L20)
16+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/49c33469b809a7ff23a1c78270c73d4eeb45cb33/_WeeklyChallenges/W02-%5BDP%5D/Assignment_BOJ_17845_%EC%88%98%EA%B0%95%EA%B3%BC%EB%AA%A9.py#L1-L20
17+
18+
> **์˜ค๋Š˜ ๋ฌธ์ œ ํ’€์ด์™€ ๊ด€๋ จํ•˜์—ฌ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ์ž๋ฃŒ ๋ชจ์Œ (๋ฌธ์ œ์— ๋Œ€ํ•œ ์ •๋‹ตX, ๊ณต๋ถ€ํ•˜๊ธฐ ์ข‹์€ ์ž๋ฃŒ):**
19+
- [๋ฐฐ๋‚ญ ๋ฌธ์ œ(KnapSack Problem) ๊ทธ๋ฆผ์œผ๋กœ ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ](https://howudong.tistory.com/106)
20+
- [[์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠธ๋ ˆ์ด๋‹] 5์žฅ - ๋™์ ๊ณ„ํš๋ฒ•๊ณผ ๋ƒ…์ƒ‰(Knapsack) (๋ฐฑ์ค€ 12865๋ฒˆ ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ ๋ฌธ์ œ๋กœ ์‚ดํŽด๋ณด๊ธฐ)](https://chanhuiseok.github.io/posts/improve-6/)
21+
- [๋‚˜๋ฌด์œ„ํ‚ค - ๋ฐฐ๋‚ญ ๋ฌธ์ œ](https://namu.wiki/w/%EB%B0%B0%EB%82%AD%20%EB%AC%B8%EC%A0%9C)
22+
23+
<img width="800" alt="แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-11-25 แ„‹แ…ฉแ„’แ…ฎ 9 21 18" src="https://github.com/user-attachments/assets/81c86394-9ee8-457b-9e64-6798a6c5ab4c">
872 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
BOJ #12865. ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ (๊ณจ๋“œ5)
3+
https://www.acmicpc.net/problem/12865
4+
์œ ํ˜•: Dynamic Programming(DP), Knapsack
5+
'''
6+
import sys
7+
input = sys.stdin.readline
8+
9+
# ์ž…๋ ฅ
10+
N, K = map(int, input().split()) # N: ๋ฌผํ’ˆ์˜ ์ˆ˜, K: ๋ฒ„ํ‹ธ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ
11+
items = [[0, 0]]
12+
for _ in range(N):
13+
items.append(list(map(int, input().split())))
14+
15+
# DP
16+
knapsack = [[0] * (K+1) for _ in range(N+1)] # DPํ‘œ๋Š” 0~K+1, 0~N+1๋กœ ๊ตฌ์„ฑ
17+
18+
for i in range(1, N+1):
19+
for j in range(1, K+1):
20+
weight = items[i][0]
21+
value = items[i][1]
22+
23+
if j >= weight: # "ํ˜„์žฌ์ตœ๋Œ€๋ฌด๊ฒŒj๊ฐ€ ํ•ด๋‹น๋ฌผ๊ฑด๋ฌด๊ฒŒ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
24+
# ํ‘œ์˜ ์œ— ์…€์˜ ๊ฐ’๊ณผ ํ˜„์žฌ๋ฌผ๊ฑด์˜V+์ด์ „๋ฌผ๊ฑด์˜V๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ DP[i][j]์— ์ €์žฅ
25+
knapsack[i][j] = max(knapsack[i-1][j], knapsack[i-1][j-weight] + value)
26+
else: # #"ํ˜„์žฌ์ตœ๋Œ€๋ฌด๊ฒŒj๊ฐ€ ํ•ด๋‹น๋ฌผ๊ฑด๋ฌด๊ฒŒ๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ (ํ˜„์žฌ ๋ฌผ๊ฑด์„ ๋‹ด์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ)
27+
# ์ด์ „ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
28+
knapsack[i][j] = knapsack[i-1][j]
29+
30+
print(knapsack[N][K])

0 commit comments

Comments
ย (0)