๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.2 : ์ˆซ์ž ๋ณ€ํ™˜ํ•˜๊ธฐ

by xxilliant 2025. 5. 31.
728x90
๋ฐ˜์‘ํ˜•

 

https://school.programmers.co.kr/learn/courses/30/lessons/154538?language=cpp

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก์˜ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 2

dfs๋กœ๋„ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ, ํšจ์œจ์„ฑ ๋•Œ๋ฌธ์— dp๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ด์ „ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•œ ๊ฐ’์ธ์ง€ ํ™•์ธ ํ›„, ์—ฐ์‚ฐํ•ด์•ผ ํ•œ๋‹ค.

 

๋‚˜์˜ ํ’€์ด

#include <string>
#include <vector>
#include <climits>
using namespace std;

int solution(int x, int y, int n) {
    vector<int> dp(y + 1, INT_MAX);
    dp[x] = 0;
    for(int i=x+1; i<=y; ++i){
        if (i - n >= x && dp[i - n] != INT_MAX)
            dp[i] = min(dp[i], dp[i - n] + 1);
        if (i % 2 == 0 && i / 2 >= x && dp[i / 2] != INT_MAX)
            dp[i] = min(dp[i], dp[i / 2] + 1);
        if (i % 3 == 0 && i / 3 >= x && dp[i / 3] != INT_MAX)
            dp[i] = min(dp[i], dp[i / 3] + 1);
    }
    if(dp[y]==INT_MAX) return -1;
    return dp[y];
}

728x90
๋ฐ˜์‘ํ˜•