Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-kokeunho #5

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

| μ°¨μ‹œ | λ‚ μ§œ | λ¬Έμ œμœ ν˜• | 링크 | 풀이 |
|:----:|:---------:|:----:|:-----:|:----:|
| 1μ°¨μ‹œ | 2024.09.28 | κ΅¬ν˜„ | [ν‚Ή](https://www.acmicpc.net/problem/1063) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/1) |
| 1μ°¨μ‹œ | 2024.09.28 | κ΅¬ν˜„ | [ν‚Ή](https://www.acmicpc.net/problem/1063) | [#1](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/1) |
| 2μ°¨μ‹œ | 2024.10.01 | λ‹€μ΄λ‚˜λ―Ή ν”„λ‘œκ·Έλž˜λ° | [ν‰λ²”ν•œ λ°°λ‚­](https://www.acmicpc.net/problem/12865) | [#5] (https://github.com/AlgoLeadMe/AlgoLeadMe-12/pulls/4) |
---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동적 κ³„νšλ²•μ„ 배우기 쒋은 예제 κ°™μŠ΅λ‹ˆλ‹€. μ˜ˆμ „ μ•Œκ³ λ¦¬μ¦˜ κ°•μ˜ λ•Œ bruteforceλ‘œλ„ ν’€μ–΄λ΄€κ³  dpλ‘œλ„ ν’€μ–΄λ΄€λ˜ 기얡이 λ‚˜λ„€μš”.
μ˜ˆμ „μ— λ°±μ€€μ—μ„œλ„ 같은 문제λ₯Ό ν’€μ–΄λ΄€μ—ˆμŠ΅λ‹ˆλ‹€. μ½”λ“œ 일뢀λ₯Ό λ³΄λ‹ˆ c++둜 ν’€μ΄ν•œ 제 μ½”λ“œλž‘ 거의 μœ μ‚¬ν•˜λ„€μš”.

int solution(int N, int K, vector<int> W, vector<int> V) {
    vector<vector<int> > opt(N + 1, vector<int>(K + 1, 0));
    for (int i = 1; i <= N; i++) {
        for (int w = 0; w <= K; w++) {
            if (w < W[i - 1])
                opt[i][w] = opt[i - 1][w];
            else
                opt[i][w] = max(opt[i - 1][w], V[i - 1] + opt[i - 1][w - W[i - 1]]);
        }
    }
    return opt[N][K];
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 λ‘œμ§μ€ λΉ„μŠ·ν•˜κ²Œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€. cpp둜 ν•˜λ‹ˆ 0-based λ°°μ—΄μ΄λž‘ 1-basedλ°°μ—΄ λ•Œλ¬Έμ— 쑰금 ν—·κ°ˆλ¦¬λ„€μš”.

전체 μ½”λ“œ
#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> weight;
vector<int> value;

void solution(vector<vector<int>>& dp) {
	for (int i = 0; i <= k; i++) dp[0][i] = 0;

	for (int i = 1; i <= n; i++) {
		for (int w = 1; w <= k; w++) {
			if (weight[i-1] > w) 
				dp[i][w] = dp[i - 1][w];
			else 
				dp[i][w] = max(dp[i - 1][w], 
					value[i-1] + dp[i - 1][w - weight[i-1]]);
		}
	}
}

int main() {
	cin >> n >> k;
	int w, v;

	weight.reserve(n);
	value.reserve(n);

	for (int i = 0; i < n; i++) {
		cin >> w >> v;
		weight.push_back(w);
		value.push_back(v);
	}
	vector<vector<int>> dp(n+1, vector<int>(k+1));
	solution(dp);
	cout << dp[n][k];
	return 0;
}

Copy link
Collaborator

@g0rnn g0rnn Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<details>
<summary>μ—¬κΈ°κ°€ 제λͺ© </summary>

```
CODE!
```
</details>

μœ„μ™€ 같이 μž‘μ„±ν•˜μ‹œλ©΄ 전체 μ½”λ“œλ₯Ό ν† κΈ€λ‘œ 넣을 수 μžˆμŠ΅λ‹ˆλ‹€!! μ°Έκ³ ν•˜μ„Έμš©

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였 λ§ˆν¬μ—… κΏ€νŒ κ°μ‚¬ν•©λ‹ˆλ‹€

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
N, K = map(int, input().split())
items = []

for _ in range(N):
W, V = map(int, input().split())
items.append((W, V))

dp = [[0] * (K+1) for _ in range(N+1)]

for i in range(1, N+1):
weight, value = items[i-1]
for j in range(1, K+1):
if j >= weight:
dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight] + value)
else:
dp[i][j] = dp[i-1][j]

print(dp[N][K])