[문제]
https://school.programmers.co.kr/learn/courses/30/lessons/154538
[풀이]
DP로 풀었다.
#include <string>
#include <vector>
using namespace std;
// 자연수 x를 y로 변환
// 1. x + n
// 2. x * 2
// 3. x * 3
// 위 세 가지 연산을 사용해서 최소 횟수로 y만들기
int solution(int x, int y, int n)
{
vector<int> DP(y + 1, INT32_MAX);
DP[x] = 0;
for (int i = x; i <= y; i++)
{
if (DP[i] == INT32_MAX) continue;
if ((i * 3) <= y) DP[i * 3] = min(DP[i * 3], DP[i] + 1);
if ((i * 2) <= y) DP[i * 2] = min(DP[i * 2], DP[i] + 1);
if ((i + n) <= y) DP[i + n] = min(DP[i + n], DP[i] + 1);
}
return DP[y] == INT32_MAX ? -1 : DP[y];
}'알고리즘 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스 Lv.2] [3차] 파일명 정렬 (0) | 2025.05.15 |
|---|---|
| [프로그래머스 Lv.2] 2xn 타일링 (0) | 2025.05.15 |
| [프로그래머스 Lv.2] 주차 요금 계산 (0) | 2025.05.14 |
| [프로그래머스 Lv.2] 스킬트리 (0) | 2025.05.13 |
| [프로그래머스 Lv.2] 택배상자 (0) | 2025.05.13 |