๐Ÿ“ Coding Test Study/Algorithm Problem

[BOJ][C++] 1463๋ฒˆ 1๋กœ ๋งŒ๋“ค๊ธฐ(feat. DP)

ibelieveinme 2024. 6. 7. 19:17
728x90

[๋ฌธ์ œ]

https://www.acmicpc.net/problem/1463

์ •์ˆ˜ x ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, x ๋ฅผ 1๋กœ ๋งŒ๋“œ๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

x ๋ฅผ 1๋กœ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

 

1) x ๊ฐ€ 3 ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด x ๋ฅผ 3 ์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.

2) x ๊ฐ€ 2 ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด x ๋ฅผ 2 ๋กœ ๋‚˜๋ˆˆ๋‹ค.

3) 1)2) ๊ฐ€ ์•„๋‹ˆ๋ฉด x ์—์„œ 1์„ ๋บ€๋‹ค.

 

์ •์ˆ˜ x ๊ฐ’์˜ ๋ฒ”์œ„๋Š” 1 ≤ x ≤ 10^6 ์ด๋‹ค.

์ œํ•œ์‹œ๊ฐ„์€ 0.15์ดˆ ์ด๋‹ค.

 

[ํ’€์ด]

์ •์ˆ˜ x ์˜ ์ตœ๋Œ€๊ฐ’์ด 10^6 ์ด๊ธฐ ๋•Œ๋ฌธ์— O(n^2) ๋งŒ ๋˜์–ด๋„ ๋ฌธ์ œ์˜ ์ œํ•œ์‹œ๊ฐ„์„ ๋„˜๊ธด๋‹ค. ์ฆ‰, ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€๋ฉด ์•ˆ๋œ๋‹ค.

 

์ ํ™”์‹์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

'D[x] = x๋ฅผ 1๋กœ ๋งŒ๋“œ๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ์˜ ์ˆ˜' ๋ผ๊ณ  ๋‘๊ณ  D[1] ๋ถ€ํ„ฐ ๊ตฌํ•ด๋ณด์ž.

 

D[1] = 1

D[2] = D[2/2] = 1

D[3] = D[3/3] = 1

D[4] = D[4/2] = D[2] = D[2/2] = 1

 

D[4] ๊นŒ์ง€๋งŒ ๊ตฌํ•ด๋ด๋„ D[2] ๋ฅผ ์žฌ์‚ฌ์šฉํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํฐ ๊ฐ’ x๋ฅผ ์ž‘์€ ๊ฐ’๋“ค์„ ์ด์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ. DP ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

๋”ฐ๋ผ์„œ D[x] ์˜ ์ ํ™”์‹์€ D[x] = min(D[x/3] + 1, D[x/2] + 1, D[x-1] + 1) = min(D[x/3], D[x/2], D[x-1]) + 1 ์ด๋‹ค.

๋‹ค๋งŒ, x๊ฐ€ 3์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ, 2๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ, 3์ด๋‚˜ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊นŒ์ง€ ์ด 3๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ๋‚˜๋ˆ ์„œ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

1) Top-down ๋ฐฉ์‹

int go(int n){
    if(n == 1) return 0;
    if(d[n] > 0) return d[n];
    
    d[n] = go(n-1) + 1;
    if(n%2 == 0) d[n] = min(d[n], go(n/2) + 1);
    if(n%3 == 0) d[n] = min(d[n], go(n/3) + 1);

    return d[n];
}

์‹œ๊ฐ„๋ณต์žก๋„: O(n)

 

2) Bottom-up ๋ฐฉ์‹

d[1] = 0;
for(int i = 2; i<= n; i++){
    d[i] = d[i-1] + 1;
    if(i%2 == 0) d[i] = min(d[i], d[i/2] + 1);
    if(i%3 == 0) d[i] = min(d[i], d[i/3] + 1);
}

์‹œ๊ฐ„๋ณต์žก๋„: O(n)

 

[์ฝ”๋“œ]

//Top-down ๋ฐฉ์‹

#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_SIZE = 1000001;
int d[MAX_SIZE]; // d[x] = x๋ฅผ 1๋กœ ๋งŒ๋“œ๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ์˜ ์ˆ˜.

int go(int x);

int main() {
	int x;

	cin >> x;
	cout << go(x) << "\n";

	return 0;
}

int go(int x) {
	if (x == 1) return 0;
	if (d[x] > 0) return d[x];

	d[x] = go(x - 1) + 1;
	if (x % 3 == 0) d[x] = min(d[x], go(x / 3) + 1);
	if (x % 2 == 0) d[x] = min(d[x], go(x / 2) + 1);

	return d[x];
}

 

 

//Bottom-up ๋ฐฉ์‹

#include <iostream>
#include <algorithm>
using namespace std;

int go(int x);

int main() {
	int x;
	
	cin >> x;
	cout << go(x);

	return 0;
}

int go(int x) {
	int* d = new int[x + 1] {0, };

	d[1] = 0;	
	for (int i = 2; i <= x; i++) {
		d[i] = d[i - 1] + 1;
		if (i % 2 == 0) d[i] = min(d[i], d[i / 2] + 1);
		if (i % 3 == 0) d[i] = min(d[i], d[i / 3] + 1);
	}

	return d[x];
}

*  DP ๊ฐœ๋… ๋ฐ ํ’€์ด๋ฒ• ์ •๋ฆฌ

https://i-believe-in-me.tistory.com/413

 

DP(Dynamic Programming) ๊ฐœ๋…, ํ’€์ด๋ฒ•

* DP(Dynamic Programming)๋ž€ ?: ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ณ  ์ž‘์€๋ฌธ์ œ๋กœ ํฐ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•. ์ž‘์€๋ฌธ์ œ๊ฐ€ ์ค‘๋ณต๋œ๋‹ค.(vs ๋ถ„ํ• ์ •๋ณต: ๋ถ„ํ• ์ •๋ณต๊ณผ ๋น„์Šทํ•œ๋ฐ ๋ถ„ํ• ์ •๋ณต์€ ์ž‘์€๋ฌธ์ œ๊ฐ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค.)  * DP

i-believe-in-me.tistory.com

 

728x90