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

9-kokeunho #33

Merged
merged 1 commit into from
Dec 10, 2024
Merged

9-kokeunho #33

merged 1 commit into from
Dec 10, 2024

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ] 계단 였λ₯΄κΈ° https://www.acmicpc.net/problem/2579

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30min

✨ μˆ˜λ„ μ½”λ“œ

  1. μš°μ„  scores에 각 κ³„λ‹¨λ§ˆλ‹€μ˜ 점수λ₯Ό λ°›μŠ΅λ‹ˆλ‹€.
  2. dp ν…Œμ΄λΈ”μ„ μƒμ„±ν•΄μ„œ 1, 2, 3번의 κ³„λ‹¨κΉŒμ§€λŠ” λ”°λ‘œ μ΅œλŒ€κ°’μ„ μž…λ ₯ν•΄μ€λ‹ˆλ‹€.
  3. κ·Έ λ’€ κ³„λ‹¨κΉŒμ§€μ˜ μ΅œλŒ€κ°’μ€ 점화식을 μ μš©ν•΄μ„œ κ΅¬ν•©λ‹ˆλ‹€.
    점화식은
    dp[i] = max(dp[i-2], dp[i-3] + scores[i-1]) + scores[i]
    
    μž…λ‹ˆλ‹€.
    (3계단 μ—°μ†μœΌλ‘œ λ°Ÿμ„ 수 μ—†λŠ” 쑰건이 μžˆμœΌλ―€λ‘œ,
    이전 계단을 밟고 온 κ²½μš°μ™€ 그렇지 μ•Šμ€ 경우둜 λ‚˜λˆ„μ–΄μ„œ
    max값을 κ΅¬ν•˜κ³  ν˜„μž¬ κ³„λ‹¨μ˜ 점수λ₯Ό λ”ν•©λ‹ˆλ‹€.)
    dp[i-3] + scores[i-1]이 이전 계단을 밟고 온 경우 μž…λ‹ˆλ‹€.
    dp[i-1]이 μ•„λ‹Œ μ΄μœ λŠ” dp[i-1]도 κ²½μš°κ°€ 두가지 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

졜근 μ—¬λŸ¬λΆ„μ΄ dp 문제λ₯Ό 많이 μ˜¬λ €μ£Όμ…”μ„œ 많이 ν’€μ–΄λ΄€λŠ”λ°μš”.
κ·ΈλŸΌμ—λ„ 15λΆ„ 정도 혼자 생각해봐도 점화식을 λ– μ˜¬λ¦¬μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜λ„ 이번 문제λ₯Ό ν’€λ©΄μ„œ μ–΄λŠ 정도 감이 μ‘°κΈˆμ”© μž‘ν˜€κ°€λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

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λ¬Έμ œμΈμ€„ μ•Œκ³  λ¬Έμ œλ„ μ œλŒ€λ‘œ μ•ˆμ½κ³  μ‹œλ„ν–ˆλŠ”λ°,
μ‹œκ°„μ΄ λ„ˆλ¬΄ λŒλ Έλ„€μš”.... 30λΆ„ κ³ λ―Όν•˜κ³  κ·Όν˜Έλ‹˜ μ½”λ“œ λ΄€μŠ΅λ‹ˆλ‹€..

처음 보고

 dp[i] = point[i] + max(dp[i - 2], point[i - 1] + dp[i - 3]);

이 점화식이 μ™œ μ„±λ¦½ν•˜λŠ”μ§€ 이해가 μ‰½μ§€λŠ” μ•Šμ•˜λ„€μš”...
근데 μ°¨κ·Όμ°¨κ·Ό 생각을 ν•˜λ‹ˆ (근데 이제 κ·Όν˜Έλ‹˜ μ„€λͺ…을 곁듀인...) λ§žλ”κ΅°μš”!

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!

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

using namespace std;

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

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

    vector<int> dp(N + 1, 0);
    dp[1] = point[1];
    if (N >= 2) dp[2] = point[1] + point[2];
    if (N >= 3) dp[3] = max(point[1] + point[3], point[2] + point[3]);

    for (int i = 4; i <= N; i++) {
        dp[i] = point[i] + max(dp[i - 2], point[i - 1] + dp[i - 3]);
    }
    cout << dp[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[i]λ₯Ό κ΅¬ν•˜λŠ” κ³Όμ •μ—μ„œ 계단을 μ—°μ†μ μœΌλ‘œ λ°Ÿμ§€ μ•Šλ„λ‘ max값을 κ΅¬ν•˜λ©° 점화식을 μƒμ„±ν•˜λŠ”κ²Œ ν•΅μ‹¬μ΄μ—ˆλ„€μš”!!
30λΆ„ ν’€λ‹€κ°€ λͺ¨λ₯΄κ² μ–΄μ„œ κ·Όν˜Έλ‹˜ μ„€λͺ…을 μ½μ—‡μŠ΅λ‹ˆλ‹€.. κ·Έλž˜μ„œ 핡심 λ‘œμ§μ€ κ°™μŠ΅λ‹ˆλ‹€!

    dp[0] = stair[0];
    if(n >= 2) dp[1] = stair[0] + stair[1];
    if(n >= 3) dp[2] = stair[2] + max(stair[0], stair[1]);

    for(int i=3; i<n; i++) {
        dp[i] = stair[i] + max(stair[i-1] + dp[i-3], dp[i-2]);
    }

Base automatically changed from 8-kokeunho to main December 10, 2024 09:09
@kokeunho kokeunho merged commit a35d051 into main Dec 10, 2024
1 check passed
@kangrae-jo kangrae-jo deleted the 9-kokeunho branch December 10, 2024 15:37
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