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

10-wnsmir #38

Merged
merged 2 commits into from
Dec 28, 2024
Merged

10-wnsmir #38

merged 2 commits into from
Dec 28, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 30, 2024

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

์ด ๋ฌธ์ œ ๋˜ํ•œ ์ฑ…์—์„œ ๋ฐœ์ทŒํ•œ ๋ฌธ์ œ๋ผ ๋งํฌ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
๋ฐ”๋กœ ์ด์ „ ์ฝ”๋“œ๋ฆฌ๋ทฐ์—์„œ ๊ฐ•๋ž˜๋‹˜์˜ dp๋ฌธ์ œ๋ฅผ ํ’€์–ด์„œ dp๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด๋ฉฐ ์•ˆํ‘ผ ๋‹ค์Œ๋ฌธ์ œ๋ฅผ ๋ณต์Šต๊ฒธ ๋ฐ”๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค p.220
์ฒซ ์ค„์— ์ •์ˆ˜ N ์ž…๋ ฅ (3<=N<=100)
๋‘˜์งธ์ค„์— ๊ฐ ์นธ์˜ ์ˆ˜ ์ž…๋ ฅ (๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฅ˜)

๋ฐ”๋กœ ์˜†์นธ์€ ์•ˆ๋˜๊ณ  ํ•œ์นธ์„ ์„ ํƒํ•˜๋ฉด ํ•œ์นธ๋–จ์–ด์ง„ ๋‹ค์Œ์นธ๋งŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜์‹œ์˜ค.
์˜ˆ๋ฅผ๋“ค์–ด 4์นธ์ด ์žˆ์œผ๋ฉด

์ž…๋ ฅ
4
1 3 1 5

์ถœ๋ ฅ
8

3๊ณผ 5๋ฅผ ์„ ํƒํ•œ 8์ด ๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.

1 3 1 5 6 ์ด ์žˆ๋‹ค๋ฉด
3 + 6 ์œผ๋กœ 9๊ฐ€ ๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.

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

15min

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

dp๋ฌธ์ œ๋Š” ์ด ๋ฌธ์ œ๊ฐ€ dp์œ ํ˜•์ธ์ง€ ์•Œ์•„๋‚ด๋Š”๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.
๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์•Œ์•„์•ผ ์ตœ๋Œ€ ์ตœ์†Œ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ ํ™”์‹์„ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด, dp๋ฌธ์ œ์ž„์„ ๋ˆˆ์น˜์ฑ„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ ๋˜ํ•œ ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ๊บผ๋ฅผ ์„ ํƒํ•˜๊ณ , ํ•˜๋‚˜๋„์›Œ์ง„ ์ „์ „๊ฒƒ์„ ์„ ํƒํ•˜๋Š”๊ฒƒ. (์ „์ „๊ฒƒ์€ ์ด๋ฏธ ์ตœ์ ํ™” ๋˜์–ด์žˆ๋Š” ์ƒํƒœ์ด๋ฏ€๋กœ ๊ทธ ์ด์ „์€ ๊ณ ๋ คํ•  ํ•„์š” ์—†์Œ)
๋˜๋Š”,
ํ˜„์žฌ๊บผ๋ฅผ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  ์ด์ „๊ฒƒ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ. (ํ˜„์žฌ ์ˆ˜๊ฐ€ ์ ๋‹ค๋ฉด ์ด์ „ ์ตœ์ ๊ฐ’์„ ์„ ํƒ ๋ชปํ•˜๊ฒŒ๋˜๋Š”๊ฒƒ์„ ๋ฐฉ์ง€)

N = int(input())

Food = list(map(int, input().split())) #๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ ์นธ์˜ ์ˆ˜๋ฅผ ๋ฐ›์•„์คŒ.

dp = [0]*(N+1) #dp๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
dp[1] = Food[0]
dp[2] = max(Food[0], Food[1])

for i in range(3, N+1):
    dp[i] = max(dp[i-2]+Food[i-1], dp[i-1])

print(dp[N])

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๋ฌธ์ œ... ์žฌ๋ฐŒ๋„ค์š”..

๋ฌธ์ œ ์„ค๋ช…์„ ์ฝ๋‹ค๊ฐ€ ํ’€์ด์—๋„ ๋ˆˆ์ด ์Šฌ์ฉ ๊ฐ€๋Š” ๋ฐ”๋žŒ์—
์‹œ๊ฐ„๋„ ๊ฝค ์งง๊ฒŒ ๊ฑธ๋ ธ๊ณ , ํ’€์ด๋„ ์ƒ๋‹นํžˆ ๋น„์Šทํ•˜๋„ค์š”.

์กฐ๊ธˆ ๋ง‰ํ˜”๋˜ ๋ถ€๋ถ„์ด๋ผ๋ฉด

dp[2] = max(input[1], input[2])
์ด๋ถ€๋ถ„ ์ž…๋‹ˆ๋‹ค.

N >= 3์ด๊ณ , dp์—์„œ i-2๋ฅผ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—
dp[2]์˜ ๊ฐ’ ๊นŒ์ง€๋Š” ์„ค์ •ํ•ด์ฃผ๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

dp[2]๋ฅผ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ• ์ง€ ์‚ด์ง ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ, ์–˜๋„ ๊ทธ๋ƒฅ ์ง์ „๊ฐ’ vs ํ˜„์žฌ๊ฐ’์œผ๋กœ ์ƒ๊ฐํ•ด์„œ ์ €๋Ÿฐ ์‹์„ ์ ์—ˆ์Šต๋‹ˆ๋‹ค!

์ฑ…์—์„œ ๋ฐœ์ทŒํ•œ ๋ฌธ์ œ๋ผ ์‹ ๊ธฐํ•˜๊ธด ํ•œ๋ฐ ์ปดํŒŒ์ผ์ด ์•ˆ๋œ๋‹ค๋Š” ์ ์ด ์‚ด์ง ์•„์‰ฝ์Šต๋‹ˆ๋‹ท
์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

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

using namespace std;

int main() {
    int N;
    cin >> N;

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

    vector<int> dp(N + 1, 0);
    dp[1] = input[1];
    dp[2] = max(input[1], input[2]);
    for (int i = 3; i <= N; i++) {
        dp[i] = max(dp[i - 1], input[i] + dp[i - 2]);
    }

    cout << dp[N] << '\n';

    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  dp๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐ์€ ํ–ˆ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐํ•œ ์ด์œ ๋ฅผ ์ •ํ™•ํžˆ ์„ค๋ช…ํ•˜์ง„ ๋ชปํ•˜๊ฒ ๋„ค์š”..
์—ฌ๋Ÿฌ ๋ฌธ์ œ๋ฅผ ๋” ํ’€๋ฉด์„œ ์–ด๋–ค ์œ ํ˜•์œผ๋กœ ํ’€์–ด์•ผํ• ์ง€๋ž‘ ๊ทธ ์ด์œ ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋ ค๊ตฌ์š”!

์ œ๊ฐ€ ํ‘ผ ์ฝ”๋“œ๋Š” ์ค€์šฉ๋‹˜์ด๋ž‘ ์‚ด์ง ๋‹ค๋ฅด๋„ค์š”. ๊ทผ๋ฐ ์ด ๋ฐฉ๋ฒ•๋„ ์ •๋‹ต์ด ๋˜๋Š”์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค..

dp[0] = arr[0];
    dp[1] = arr[1];
    for (int i = 2; i < n; i++)
        dp[i] = arr[i] + max(dp[i - 2], dp[i - 3]);
    cout << max(dp[n - 1], dp[n - 2]);

์ €๋Š” dp ๋ฐฐ์—ด์— ๋Œ€ํ•ด i๋ฒˆ์งธ ์œ„์น˜ํ•œ ๊ฐ’์€ 0~i๊นŒ์ง€์˜ ์ตœ๋Œ€ํ•ฉ์ด๋‹ค. ๋ผ๋Š” ๊ทœ์น™์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ๊ฐ€๋งŒํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์ค€์šฉ๋‹˜ ์ฒ˜๋Ÿผ ํ•˜๋‚˜๋กœ ํ•ฉ์น  ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋„ค์š”

@kokeunho
Copy link
Collaborator

์˜ˆ์ „์— ์ค€์šฉ๋‹˜์ด ํ‘ธ์…จ๋˜ ์นธ ๋„˜๊ธฐ? ๋ฌธ์ œ์˜ ํ•˜์œ„ ๋ฒ„์ „์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
dp ๋ฌธ์ œ๋ฅผ ๊ทธ๋‚˜๋งˆ ๋งŽ์ด ํ’€์–ด๋ดค๋Š”๋ฐ ์•„์ง ์ ํ™”์‹์„ ๋– ์˜ฌ๋ฆฌ๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฝค๋‚˜ ๋“œ๋„ค์š”.
๊พธ์ค€ํžˆ ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

์ฝ”๋“œ
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] dp = new int[n+1];

        dp[1] = sc.nextInt();
        dp[2] = sc.nextInt();

        for(int i = 3; i <= n; i++) {
        int num = sc.nextInt();
        dp[i] = Math.max(num + dp[i-2], dp[i-1]);
        }

        System.out.println(dp[n]);
    }
}

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.

4 participants