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

11-g0rnn #49

Merged
merged 3 commits into from
Jan 8, 2025
Merged

11-g0rnn #49

merged 3 commits into from
Jan 8, 2025

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Jan 2, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๊ธฐํƒ€๋ฆฌ์ŠคํŠธ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40 - 50m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด ๋Œ€๋†“๊ณ  dp๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. i๋ฒˆ์งธ ๋ณผ๋ฅจ์„ ์กฐ์ ˆํ•  ๋•Œ๋งˆ๋‹ค ์ด์ „์˜ ๊ฐ’์— ์˜์กด๋˜๊ณ  ์ตœ์ ์˜ ํ•ด๋ฅผ ๊ตฌํ•œ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ dp๋ฌธ์ œ์ž„์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์ฒ˜์Œ์— 1์ฐจ์› dp๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ํ’€์–ด์™”๋˜ ๋ฐฉ์‹๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์ ํ™”์‹์„ ์„ธ์šฐ๊ณ  ์ตœ๋Œ“๊ฐ’์— ๋Œ€ํ•œ ์ œ์•ฝ๋งŒ ์„ธ์šฐ๋ฉด ๋ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ๊ฑฐ๋“ ์š”. ํ•˜์ง€๋งŒ ์ด๋Š” ์ด์ „์˜ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ด์ „์˜ ์ตœ๋Œ“๊ฐ’๋“ค๋งŒ ๊ณ ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์ „ ์ƒํƒœ์˜ ๋ชจ๋“  ๋ณผ๋ฅจ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด 2์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ
3 5 10
5 3 7
image

์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•ˆ๋˜๋Š” ๊ฒƒ๋“ค์€ 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ m๋ณด๋‹ค ํฐ ๊ฐ’๋“ค์ด๊ณ  ์ด๋ฅผ ์ง€์šฐ๋ฉด

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-01-02 แ„‹แ…ฉแ„’แ…ฎ 3 28 20

์™€ ๊ฐ™์ด ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์œ„์˜ ํŠธ๋ฆฌ์ฒ˜๋Ÿผ ๊ฐ’์„ ๊ณ„์‚ฐํ•œ ํ›„ ๊ฐ’์˜ ๊ฐ€๋Šฅ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ทธ์ค‘ ์ œ์ผ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์ง€๊ธฐ ์ „์— ๋ณผ๋ฅจ ๊ฐ’์ด ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„ ๋‹ค๋ฉด false๋กœ ์„ค์ •ํ•˜์—ฌ ์ดํ›„๋ฅผ ๊ณ„์‚ฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

dp๋ฅผ ํ’€ ๋•Œ ๊ฐ’์— ์˜์กดํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

์ €ํ•œํ…Œ๋Š” ์กฐ๊ธˆ ์–ด๋ ค์šด dp ์˜€๋„ค์š”..
40๋ถ„ ์ •๋„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๊ท ํ˜ธ๋‹˜ ์ฝ”๋“œ ๋ณด๊ณ  ์ˆ˜์ •ํ•ด์„œ ๊ฒจ์šฐ ๋งž์•˜์Šต๋‹ˆ๋‹ค

์ผ๋‹จ ์ €๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ์“ธ ์ƒ๊ฐ์„ ์•ˆํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ํ–ˆ๋Š”๋ฐ i-2๋ฒˆ์งธ ๊ณผ๊ฑฐ๋กœ๋งŒ ๋Œ์•„๊ฐ„๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

for (int i = 2; i <= N; i++) {
        dp[i] = dp[i - 1] + volume[i - 1];

        if (dp[i] > M) {
            dp[i] = dp[i - 2] - volume[i - 2] + volume[i - 1];
        }
        if (dp[i] < 0) {
            dp[N] = -1;
            break;
        }
}

2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผํ•˜๋‚˜? ํ•˜๊ณ  ๊ณ„์† ์‹œ๋„ํ–ˆ์ง€๋งŒ ์‹คํŒจ...
๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ M <= 1000 ์ด์ •๋„ ๋”๋ผ๊ตฌ์š”
์ด์ •๋„๋ฉด 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์€ ๋ฐฉ์‹์ด๊ฒ ๋‹ค ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๋˜ 2์ฐจ์› ๋ฐฐ์—ด์— bool ๊ฐ’์„ ์ €์žฅํ•ด์„œ true์ธ ๊ฒƒ ์ค‘์— ๊ฐ€์žฅ ํฐ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค๋Š” ๊ฒƒ ๋„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.
์ €๋Š” dp๋ฅผ ํ’€ ๋•Œ bool ๊ฐ’์„ ๋„ฃ์€๊ฑด ์ฒ˜์Œ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”.
์ข‹์€๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ~~

CPP CODE
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int N, S, M;
    cin >> N >> S >> M;

    vector<int> volume(N);
    for (int i = 0; i < N; i++) {
        cin >> volume[i];
    }

    vector<vector<bool>> dp(N + 1, vector<bool>(M + 1, false));
    dp[0][S] = true;

    for (int i = 1; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            if (dp[i - 1][j]) {
                if (j + volume[i - 1] <= M) dp[i][j + volume[i - 1]] = true;
                if (j - volume[i - 1] >= 0) dp[i][j - volume[i - 1]] = true;
            }
        }
    }

    int result = -1;
    for (int j = M; j >= 0; j--) {
        if (dp[N][j]) {
            result = j;
            break;
        }
    }
    cout << result;

    return 0;
}


for (int i = 2; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (!dp[i - 1][j]) continue;
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ๊น”๋”ํ•˜๊ณ  ์ข‹๋„ค์š” ๐Ÿ‘

@kokeunho
Copy link
Collaborator

kokeunho commented Jan 7, 2025

dp๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ boolean ๋ฐฐ์—ด์„ ์“ด ๊ฑด ์ฒ˜์Œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ํ—ค๋งค๋‹ค๊ฐ€ ๊ท ํ˜ธ๋‹˜๊ฑธ ๋‹ค ๋ณด๊ณ  ํ•ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.
์„ค๋ช…๋„ ์ •๋ง ์ž˜ํ•ด์ฃผ์…จ๋„ค์š”
์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

java code
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int s = sc.nextInt();
        int m = sc.nextInt();
        int[] v = new int[n+1];

        boolean[][] dp = new boolean[n+1][m+1];
        dp[0][s] = true;

        for (int i = 1; i <= n; i++) {
            v[i] = sc.nextInt();
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (dp[i-1][j]) {
                    if(j + v[i] <= m) {
                        dp[i][j + v[i]] = true;
                    }
                    if(j - v[i] >= 0) {
                        dp[i][j - v[i]] = true;
                    }
                }
            }
        }

        int max = -1;

        for (int j = 0; j <= m; j++) {
            if (dp[n][j] == true) {
                max = j;
            }
        }
        System.out.println(max);
    }
}

@g0rnn g0rnn merged commit 39918a0 into main Jan 8, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants