์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- ๋์ ํฉ
- BFS
- ๊ตฌํ
- ๋ฐฑํธ๋ํน
- ์ต๋จ ๊ฒฝ๋ก
- web
- ์์ ์ ๋ ฌ
- ๋ถํ ์ ๋ณต
- ๋ฌธ์์ด
- ์ฐ์ ์์ ํ
- java
- Reversing
- ๊ทธ๋ฆฌ๋
- dfs
- ์ด๋ถ ํ์
- DP
- ์คํ
- ์ฌ๊ท
- ์๋ฎฌ๋ ์ด์
- c++
- JPA
- OS
- error
- ๋ฐ์ดํฌ์คํธ๋ผ
- dynamic debugging
- GCP
- CVE
- ๋งต
- thymeleaf
- Spring
- Today
- Total
hades
[Baekjoon] 1463๋ฒ: 1๋ก ๋ง๋ค๊ธฐ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1463
๐ ์ค๊ณ
์๊ฐ ์ ํ์ด 0.15์ด์ด๊ณ , N์ด ์ต๋ 1000000์ด๋ฏ๋ก, ๋ณดํต 1์ด ๋น 1์ต ๋ฒ์ ์ฐ์ฐ์ ํ๊ธฐ ๋๋ฌธ์ ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, Ω(N^2)์ ์๊ฐ ๋ณต์ก๋๋ก ์๊ฐ ์ ํ์ ์ด๊ณผํ๋ค. N์์ ์์ํ๋ ๊ฒ์ด ์๋๋ผ, ๋ฐ๋๋ก 1์์ ์์ํ์ฌ ๋ฌธ์ ์์ ์ฃผ์ด์ง ์ฐ์ฐ์ ๋ฐ๋๋ก ํ์ฌ ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ์ค๊ณํ๋ค๋ฉด, O(N)์ผ๋ก ํด๊ฒฐํ ์ ์๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, INF = 1e9+7;
vector<int> dp(1000001, INF);
int main() {
cin >> n;
dp[1] = 0;
for (int i=1; i<n; i++){
if (i*3 <= 1000000){
dp[i*3] = min(dp[i*3], dp[i]+1);
}
if (i*2 <= 1000000){
dp[i*2] = min(dp[i*2], dp[i]+1);
}
if (i+1 <= 1000000){
dp[i+1] = min(dp[i+1], dp[i]+1);
}
}
cout << dp[n] << "\n";
return 0;
}
dp[i]๋ i๋ก๋ถํฐ 1์ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ์ฐ์ฐ์ ์ต์ ํ์๋ฅผ ์ ์ฅํ๋ค. ์ฐ์ฐ์ ์ต์ ํ์๋ฅผ ๊ณ์ฐํ ๋, ์ธ๋ฑ์ค๊ฐ ๋ฐฐ์ด์ ๋ฒ์๋ฅผ ์ด๊ณผํ ์ ์์ผ๋ฏ๋ก, ์กฐ๊ฑด์ ๋ฌ์์ฃผ์ด์ผ ํ๋ค.
๐ ๋ฉ๋ชจ
๋ฐ๋๋ก๋ ์๊ฐํด๋ณด๋ ์ต๊ด ๊ธฐ๋ฅด๊ธฐ
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2024.07.02 |
---|---|
[Baekjoon] 1003๋ฒ: ํผ๋ณด๋์น ํฉ (0) | 2024.07.02 |
[Baekjoon] 1920๋ฒ: ์ ์ฐพ๊ธฐ (0) | 2024.07.01 |
[Baekjoon] 1926๋ฒ: ๊ทธ๋ฆผ (0) | 2024.07.01 |
[Baekjoon] 11047๋ฒ: ๋์ 0 (0) | 2024.07.01 |