Skip to content
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

11-dhlee777 #48

Merged
merged 1 commit into from
May 22, 2024
Merged

11-dhlee777 #48

merged 1 commit into from
May 22, 2024

Conversation

dhlee777
Copy link
Contributor

πŸ”— 문제 링크

1λ‘œλ§Œλ“€κΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ 30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μ–΄λ–€μˆ˜λ₯Ό 1λ‘œλ§Œλ“œλŠ”λ° κ±Έλ¦¬λŠ” μ΅œμ†Œ μ—°μ‚°νšŸμˆ˜λ₯Ό κ΅¬ν•˜λŠ” 문제둜 dpλ₯Ό μ΄μš©ν•΄μ„œ ν’€μ–΄λ³΄μ•˜λ‹€.
μ—°μ‚°μ˜ μ’…λ₯˜λŠ” 총 3κ°€μ§€λ‘œ
1. 1을뺀닀
2. 3μœΌλ‘œλ‚˜λˆ„μ–΄λ–¨μ–΄μ§€λ©΄ 3으둜 λ‚˜λˆˆλ‹€.
3. 2λ‘œλ‚˜λˆ„μ–΄ 떨어지면 2둜 λ‚˜λˆˆλ‹€.

μš°μ„  νŠΉμ •μˆ˜ i(1<=i<=10^6) μ—μ„œμ˜ 1λ‘œλ§Œλ“œλŠ”λ° ν•„μš”ν•œ μ—°μ‚°νšŸμˆ˜λ₯Ό μ €μž₯ν•˜λŠ” λ°°μ—΄ dp[i]λ₯Ό μ„ μ–Έν•΄μ€€λ‹€.
그리고 iκ°€ 1μΌλ•Œ μ—°μ‚°νšŸμˆ˜λŠ” 0 , 2μΌλ•ŒλŠ” 1, 3μΌλ•Œλ„ 1μ΄λ―€λ‘œ μ΄ˆκΈ°κ°’ 섀정을 ν•΄μ€€λ‹€.(dp[1]=0;dp[2]=1,dp[3]=1)

그리고 1번 μ—°μ‚° dp[i]=dp[i-1]+1 을 기본으둜 μ„€μ •ν•œ λ‹€μŒ iκ°€ 3으둜 λ‚˜λˆ„μ–΄λ–¨μ–΄μ§„λ‹€λ©΄ dp[i] = min(dp[i], dp[i / 3] + 1) λ₯Ό
톡해 κΈ°λ³Έκ°’κ³Όμ˜ λŒ€μ†ŒλΉ„κ΅, iκ°€ 2둜 λ‚˜λˆ„μ–΄λ–¨μ–΄μ§„λ‹€λ©΄ dp[i] = min(dp[i], dp[i / 2] + 1) λ₯Ό 톡해 μ΅œμ†Ÿκ°’μ„ μ°Ύμ•„μ€€λ‹€.

int dp[1000001];
int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int num;
	cin >> num;
	dp[1] = 0;
	dp[2] = 1;
	dp[3] = 1;
	for (int i = 4; i <= num; 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);
	}
	cout << dp[num];

}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

dpλ₯Ό μ˜€λžœλ§Œμ— ν’€μ–΄λ΄€λŠ”λ° 점화식을 생각해내기에 μ—¬μ „νžˆ λ§Žμ€μ‹œκ°„μ΄ κ±Έλ¦¬λŠ” 것 κ°™λ‹€.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제 1년전에 ν’€μ—ˆλ˜κ±΄λ° μ €λž‘ λ˜‘κ°™μ΄ ν‘Έμ…¨λ”λΌκ΅¬μš”..
보톡 dpλ¬Έμ œν•˜λ©΄ ν˜„μž¬ μˆ˜μ—μ„œ κ°μ†Œν•˜κ±°λ‚˜ 맨 μ²˜μŒλΆ€ν„° μ‹œμž‘ν•΄μ„œ 점점 μŒ“μ•„κ°€λŠ” ν˜•μ‹μœΌλ‘œ νƒμƒ‰ν–ˆλŠ”λ° 이 번문제 μ—­μ‹œ 1, 2, 3을 1둜 μž‘κ³ ν•˜λŠ” κ²ƒμ—μ„œ ꡉμž₯히 μ°ΎλŠ”λ° 어렀움이 μžˆμ—ˆλ˜ 것 κ°™μŠ΅λ‹ˆλ‹€.
ν•΄λ‹Ή 뢀뢄을 잘 작고 μ²΄ν¬ν•΄μ„œ μ’‹μ•˜λ˜ 것 κ°™μŠ΄λ‹€! λ‹€μŒμ—λŠ” μ°Έκ³ ν•œ λ‚΄μš©λ„ 같이 μ˜¬λ €μ£Όμ‹œλ©΄ λΉ„κ΅ν•΄λ³΄λ©΄μ„œ 보기 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DPλŠ” 항상 λ¬Έμ œμ™€ 닡에 λŒ€ν•œ 이해λ₯Ό λ™λ°˜ν•΄μ•Ό 점화식이 λ‚˜μ˜€λ”λΌκ΅¬μš”.. dp 문제λ₯Ό λŒ€ν•  λ•ŒλŠ” 항상 νŽœμ„ λ¨Όμ € μž‘μ•„μ„œ 점화식뢀터 μ°ΎλŠ”κ²Œ 더 빨리 ν‘ΈλŠ” 것 κ°™μ•„μš” :)

μ œκ°€ μ˜ˆμ „μ— ν’€μ—ˆλ˜ μ½”λ“œλž‘λ„ λΉ„μŠ·ν•˜λ„€μš”! λ‹€λ₯Έ 점은 μ‹œμž‘ 값을 μ„€μ • μ•ˆν•œ 것 정도인 것 κ°™λ„€μš”... μ™œ μ €λŠ” λ”°λ‘œ 값을 μ„€μ • μ•ˆν–ˆλŠ”λ° λ™μž‘ν•˜λŠ” κ±ΈκΉŒμš”? μ € aλŠ” λ­˜κΉŒμš”..?

#include <iostream>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

using namespace std;

int main(){
    fastio;
    int n, a = 1; cin >> n;
    int arr[n + 1] = {0, };
    for (int i = 1; i <= n; i++){
        arr[i] = arr[i-1] + 1;
        if (i % 2 == 0) {arr[i] = min(arr[i], arr[i/2] + 1);}
        if (i % 3 == 0) {arr[i] = min(arr[i], arr[i/3] + 1);}
    }
    cout << arr[n] - 1;
}

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 숫자λ₯Ό μ²˜μŒλΆ€ν„° μ μœΌλ©΄μ„œ μ–΄λ–»κ²Œ dpκ°€ μ§„ν–‰λ˜λŠ”μ§€ ν™•μΈν•˜λ©΄μ„œ ν’€μ—ˆμ–΄μš”. 점화식을 잘 μ°Ύμ•„μ„œ 잘 ν‘Έμ‹ λ“―ν•©λ‹ˆλ‹€.

@dhlee777 dhlee777 merged commit 98309d4 into main May 22, 2024
1 check passed
@dhlee777 dhlee777 deleted the 11-dhlee777 branch May 22, 2024 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants