-
Notifications
You must be signed in to change notification settings - Fork 0
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
7-kangrae-jo #24
7-kangrae-jo #24
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dpλ ν리면 μ λ§ μ¬λ°λκ±° κ°λ€μ γ
γ
γ
κ°λλμ΄λ λΉμ·νμ§λ§ μ‘°κΈ λ€λ₯Έ μ½λλ₯Ό κ°μ Έμμ΅λλ€. μ λ°μ μΈ λ‘μ§μ λΉμ·νλ λκ° κ΅¬νμ΄ naiveνκ±° κ°λ€μ π’
naive code
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
int dp[1000001];
int divide3(int k) {
if (k % 3 == 0) return k / 3;
return 0;
}
int divide2(int k) {
if (k % 2 == 0)return k / 2;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
dp[0] = INT_MAX;
dp[1] = 0;
dp[2] = 1;
dp[3] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = min({ dp[i - 1], dp[divide2(i)], dp[divide3(i)] }) + 1;
}
cout << dp[n];
return 0;
}
|
||
vector<int> dp(N + 1, 0); | ||
dp[1] = 0; | ||
dp[2] = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ€μ μκ°ν΄λ³΄λ ν΄λ΄€μ dp[i-1]
λ°μ νμΈμνλ μ΄κΈ°νλ 2κΉμ§λ©΄ μΆ©λΆνλ€μ
for (int i = 3; i <= N; i++) { | ||
dp[i] = dp[i-1] + 1; | ||
if (i % 3 == 0) dp[i] = min(dp[i], dp[i / 3]+1); | ||
if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2]+1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ½λκ° λ§€μ° κ°κ²°νλ€μ! μ΄μ°¨νΌ κ²°κ΅ dp[i]
λ₯Ό setνκ² λλ 미리 μ§μ νκ³ λ μμ κ°μΌλ‘ λΆλ₯νλ©΄ μ΄λ° μ½λκ° λμ¬ μ μλ€μ
DP λ¬Έμ λ νμ λμ?μ΄ μ½κ² λ μ€λ₯΄μ§ μλ€μ.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DPλ¬Έμ λ₯Ό νΈλ κ°μ₯ 첫λ²μ§Έ λ¨κ³λ λΉμ°νκ² μ§λ§ μ£Όμ΄μ§ λ¬Έμ κ° λ€μ΄λλ―Ή νλ‘κ·Έλλ° μ νμμ νμ νλκ²μ΄λΌ μκ°ν©λλ€.
μ΄λ¬Έμ λ κ°κ°μ μ°μ°λ°©λ²μ΄ μ£Όμ΄μ§κ³ , κ·Έ λ°©λ²λ€μ μ΄μ©ν΄ 1μ λ§λ€μ΄λ΄λ λνμ μΈ DPμ μμ λ¬Έμ μ λλ€. DP곡λΆν λ μ μΌμ²μ λ§λ¬λ λ¬Έμ μμ΄μ λΉ λ₯΄κ² ν μ μμμ΅λλ€.
+dpλ¬Έμ λ νλ€μ΄(μ¬κ·), λ°ν μ (λ°λ³΅) λκ°μ§λ‘ ν μ μλλ° λ°λ³΅λ¬ΈμΌλ‘ νΈλκ²μ΄ μκ°λ³΅μ‘λλ©΄μμλ μμν λΉ λ©΄μμ μ¬λ°λ₯Έ νμ΄λ²μ λλ€.
λ°λΌμ λ°ν μ λ°©μμ λ°λ³΅λ¬Έ ννλ‘ λ¬Έμ λ₯Ό νμ΄λ³΄μμ΅λλ€.
N = int(input())
d = [0]*10000001
for i in range(2, N+1):
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)
print(d[N])
π λ¬Έμ λ§ν¬
1λ‘ λ§λ€κΈ°
βοΈ μμλ μκ°
27m
β¨ μλ μ½λ
DPλ‘ λΆλ₯λ λ¬Έμ μΈ κ²μ μκ³ λ΄€μ§λ§,
κΆκΈν΄μ μ²μμλ κ·Έλ₯ ꡬνμΌλ‘ νμ΄λ΄€μ΅λλ€.
μ΄λ°μμΌλ‘μ.
κ·Όλ° λ¬Έμ κ° μλ€λ κ²μ μμμ΅λλ€.
3μ λλκ±°λ 2λ₯Ό λλκ±°λ 1μ λΉΌλ κ²μ μμκ° μ겨λ²λ¦°λ€λ μ μ λλ€.
μ μ½λλλ‘λ©΄,
N = 10 μΌλ, 10 -> 5 -> 4 -> 2 -> 1 λ‘ 5λ¨κ³κ° νμν©λλ€.
κ·Όλ° μ λ΅μ , 10 -> 9 -> 3 -> 1 λ‘ 4λ¨κ³λ§ μμΌλ©΄ λμ£ .
κ·Έλμ dpλ°°μ΄μ λ§λ€μ΄μ if~else λ¬Έμ΄ μλλΌ,
ifλ¬ΈμΌλ‘ λ§λ€μ΄ μ λΆ κ²μ¬νλλ‘ νμ΅λλ€.
κ·Έλ¦¬κ³ κ° if λ¬Έμμλ min κ°μ μ°Ύμμ dpλ°°μ΄μ μ μ₯νλ λ‘μ§μ μΆκ°νμ΅λλ€.
DPλ¬Έμ λ μ νμμ΄ ν΅μ¬μ΄λ μ νμμ λνλ΄κ³ pr λ§μΉκ² μ΅λλ€.
(i/2, i/3μ λν΄μλ κ°κ° 2μ 3μΌλ‘ λλμ΄ λ¨μ΄μ ΈμΌ κ²μ¬ κ°λ₯)
π μλ‘κ² μκ²λ λ΄μ©
κ·Έλ₯ μκ°λλ λλ‘ λ¨Όμ ꡬνμ ν΄λ³΄λ κ²λ λμμ§ μμ λ°©λ²μΈ κ² κ°μ΅λλ€.
μ²μ μκ°ν΄λΈ λ‘μ§μ λ¬Έμ κ° λμ§ λ³΄μ΄λκΉ,
μ λ΅μΌλ‘ ν₯νκΈ° μ¬μ΄ κ² κ°κΈ° λλ¬Έμ λλ€.