Skip to content

Conversation

@caucsejunseo
Copy link
Collaborator

@caucsejunseo caucsejunseo commented May 5, 2025

πŸ”— 문제 링크

1둜 λ§Œλ“€κΈ°

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

1h

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

이전에 ν˜„μ„λ‹˜μ΄ ν•˜μ…¨λ˜ dp문제λ₯Ό λ‹€μ‹œ ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€. #22
κ·Έλ•Œλ³΄λ‹€ 훨씬 κ°„λ‹¨ν•œ λ¬Έμ œμž…λ‹ˆλ‹€.

  1. Xκ°€ 3으둜 λ‚˜λˆ„μ–΄ λ–¨μ–΄μ§€λ©΄, 3으둜 λ‚˜λˆˆλ‹€.
  2. Xκ°€ 2둜 λ‚˜λˆ„μ–΄ λ–¨μ–΄μ§€λ©΄, 2둜 λ‚˜λˆˆλ‹€.
  3. 1을 λΊ€λ‹€.
    이 μ„Έκ°€μ§€ μ—°μ‚°λ§Œ μ‚¬μš©ν•˜μ—¬ κ°€μž₯ 적은 연산을 μ‚¬μš©ν•˜λŠ” 방법을 μ°ΎλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

μ²˜μŒμ—” λ‹¨μˆœνžˆ κ·Έλƒ₯ 3으둜 많이 λ‚˜λˆ„λ©΄ λ˜λŠ”κ±° μ•„λ‹Œκ°€ μƒκ°ν–ˆμ—ˆλŠ”λ° 경우의 μˆ˜κ°€ 훨씬 λ§Žλ”λΌκ΅¬μš”.
그리고 κ°€μž₯ 큰 μˆ˜λΆ€ν„° μ•„λž˜λ‘œ λ‚΄λ €κ°€λ©΄μ„œ 문제λ₯Ό ν•΄κ²°ν•˜λ €κ³  ν•˜λ‹€λ³΄λ‹ˆ 이것도 경우의 μˆ˜κ°€ 훨씬 λ§Žμ•„μ„œ μ‹€νŒ¨ν–ˆμ—ˆμŠ΅λ‹ˆλ‹€...

κ·Έλž˜μ„œ dp 방식을 μ‚¬μš©ν•΄μ„œ ν’€κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
μ—¬κΈ°μ„œ dp[i]λŠ” iλΌλŠ” 숫자λ₯Ό 1둜 λ§Œλ“€κΈ° μœ„ν•œ μ΅œμ†Œν•œμ˜ μ—°μ‚° 횟수λ₯Ό λ§ν•©λ‹ˆλ‹€. dp[10]이면 3인거죠.
dpλŠ” 이전에 κ³΅λΆ€ν–ˆλ“―μ΄ μ΄μ „μ˜ 연산을 κΈ°μ–΅ν•˜λ©° ν‘ΈλŠ” λ°©μ‹μž…λ‹ˆλ‹€.
image
μœ„ μ΄λ―Έμ§€μ²˜λŸΌ μ•žμ„  dp[1]λΆ€ν„° dp[9]κΉŒμ§€λŠ” ν•΄κ²°λ˜μ—ˆλ‹€κ³  μƒκ°ν•˜κ³  κ·Έ μ•žμ„  값듀을 κΈ°μ–΅ν•˜κ³  ν‘ΈλŠ” λ°©μ‹μž…λ‹ˆλ‹€.

κ·Έλž˜μ„œ dp[10]을 ꡬ해야 ν•œλ‹€κ³  κ°€μ •ν•œλ‹€λ©΄ dp[9]의 값을 κΈ°μ–΅ν•˜κ³  μ•Œκ³  μžˆμœΌλ―€λ‘œ dp[10]은 dp[9]μ—μ„œ μ—°μ‚° 횟수인 +1을 ν•œ dp[10]=dp[9]+1μ΄λΌλŠ” 식이 성립할 수 μžˆμŠ΅λ‹ˆλ‹€. 또 λ‹€λ₯Έ 식은 dp[10]=dp[5]+1이 될 수 μžˆμŠ΅λ‹ˆλ‹€. 즉 5λ₯Ό 1둜 λ§Œλ“œλŠ” μ—°μ‚° νšŸμˆ˜μ—μ„œ +1을 ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€. μ™œλƒν•˜λ©΄ 10을 2둜 λ‚˜λˆŒ 수 있고 그러면 값이 5κ°€ 되기 λ•Œλ¬Έ!

이걸 μ ν™”μ‹μœΌλ‘œ μž‘μ„±ν•˜λ©΄

dp[i] = dp[i - 1] + 1         // 기본적으둜 1을 λΊ€ 경우

    if i % 2 == 0:                // 2둜 λ‚˜λˆ μ§ˆ 경우
        dp[i] = min(dp[i], dp[i / 2] + 1)

    if i % 3 == 0:                // 3으둜 λ‚˜λˆ μ§ˆ 경우
        dp[i] = min(dp[i], dp[i / 3] + 1)

μ΄λ ‡κ²Œ λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‹ˆκΉŒ μ²˜μŒλΆ€ν„° 10μ—μ„œ 3도 λ‚˜λˆ λ³΄κ³  1도 빼보고 ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ dp[1]λΆ€ν„° μ°¨λ‘€λ‘œ ν’€μ–΄λ‚˜κ°€λŠ” λ°©μ‹μž…λ‹ˆλ‹Ή

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

'''μž…λ ₯: μ •μˆ˜ N

λ°°μ—΄ dp[1..N]을 생성
dp[1] = 0 // 1은 μ—°μ‚° 없이 1μ΄λ―€λ‘œ μ΄ˆκΈ°κ°’

for i = 2 to N:

dp[i] = dp[i - 1] + 1         // 기본적으둜 1을 λΊ€ 경우

if i % 2 == 0:                // 2둜 λ‚˜λˆ μ§ˆ 경우
    dp[i] = min(dp[i], dp[i / 2] + 1)

if i % 3 == 0:                // 3으둜 λ‚˜λˆ μ§ˆ 경우
    dp[i] = min(dp[i], dp[i / 3] + 1)

좜λ ₯: dp[N]
'''

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

dp방식을 λ‹€μ‹œ ν•œλ²ˆ κ³΅λΆ€ν•΄λ³΄κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€!!

Copy link
Contributor

@dohyeondol1 dohyeondol1 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λ₯Ό 배우게 ν–ˆλ˜ 문제인게 κΈ°μ–΅λ‚˜λ„€μš”..

저도 같은 λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.
기본적으둜 dp[i] = dp[i-1] + 1둜 μ„€μ •ν•˜κ³ ,
3 λ˜λŠ” 2둜 λ‚˜λˆ„μ–΄μ§„λ‹€λ©΄ dp[i/3]+1κ³Ό 기쑴의 dp[i]λ₯Ό 비ꡐ(2도 같은 방식)ν•˜μ—¬ μ΅œμ†Ÿκ°’μ„ μ €μž₯ν•˜λŠ” ν’€μ΄μž…λ‹ˆλ‹€.

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N;
    cin >> N;
    
    vector<int> dp(N+1);
    dp[1] = 0;
	for (int i = 2; 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);
	}
    
    cout << dp[N] << '\n';
    
    return 0;
}

μΆ”κ°€λ‘œ, μ€€μ„œλ‹˜μ˜ μ½”λ“œλ₯Ό μ œκ°€ 쑰금 더 κ°„λ‹¨ν•˜κ²Œ μˆ˜μ •ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

C99
#include <stdio.h>

int main(){
    int dp[1000001], n;
    scanf("%d", &n);
    
    dp[1] = 0;
    for(int i = 2; i <= n; i++){
        dp[i] = dp[i-1] + 1;
        if(i%2 == 0 && dp[i] > dp[i/2]+1) dp[i] = dp[i/2]+1;
        if(i%3 == 0 && dp[i] > dp[i/3]+1) dp[i] = dp[i/3]+1;
    }
    printf("%d", dp[n]);
}

μŠ€νƒ€μΌ 차이인 뢀뢄도 μžˆμ§€λ§Œ,
λΆˆν•„μš”ν•œ λ©”λͺ¨λ¦¬ ν• λ‹Ή 체크 μ½”λ“œλž‘ λ°°μ—΄ λ©”λͺ¨λ¦¬ 동적 ν• λ‹Ή μ½”λ“œλ₯Ό μ§€μ›Œλ΄€μ–΄μš”.
μ•„λ¬΄λž˜λ„ 문제λ₯Ό ν’€ λ•Œ, 짧은 μ½”λ“œ μž‘μ„±μ€ μ‹œκ°„μ„ λ‹¨μΆ•μ‹œμΌœμ€˜μ„œ μ–΄λŠμ •λ„ 이점이 μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.
λ¬Όλ‘  μžκΈ°κ°€ 제일 잘 μ“°λŠ” 방법이 μž₯λ•‘μž…λ‹ˆλ‹€ γ…Žγ…Ž

이 λ¬Έμ œλž‘ λΉ„μŠ·ν•œ 1, 2, 3 λ”ν•˜κΈ° λΌλŠ” λ¬Έμ œλ„ ν•œλ²ˆ ν’€μ–΄λ³΄μ‹œλŠ” κ±Έ μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€!
DP λ¬Έμ œλŠ” μ ν™”μ‹λ§Œ μ„ΈμšΈ 수 있으면 μ‰½κ²Œ ν’€λ¦¬λŠ”λ° κ·Έ 점화식이... 항상 문제인 것 κ°™μ•„μš” γ…Žγ…Ž;;

| 8μ°¨μ‹œ | 2025.04.11 | μ—°κ²°λ¦¬μŠ€νŠΈ| [ν‚€λ‘œκ±°](https://www.acmicpc.net/problem/5397)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/30|
| 8μ°¨μ‹œ | 2025.04.11 | μ—°κ²°λ¦¬μŠ€νŠΈ| [ν‚€λ‘œκ±°](https://www.acmicpc.net/problem/5397)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/30|
| 9μ°¨μ‹œ | 2025.04.30 | λ¬Έμžμ—΄ | [λ“£λ³΄μž‘](https://www.acmicpc.net/problem/1764)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/35|
|10μ°¨μ‹œ| 2025.05.05 | λ‹€μ΄λ‚˜λ―Ή | [1λ‘œλ§Œλ“€κΈ°](https://www.acmicpc.net/problem/1463)||
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
|10μ°¨μ‹œ| 2025.05.05 | λ‹€μ΄λ‚˜λ―Ή | [1λ‘œλ§Œλ“€κΈ°](https://www.acmicpc.net/problem/1463)||
|10μ°¨μ‹œ| 2025.05.05 | DP | [1λ‘œλ§Œλ“€κΈ°](https://www.acmicpc.net/problem/1463)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/38|

PR 링크가 λΉ μ Έμžˆλ„€μš”! λ˜ν•œ μœ ν˜•μ€ 'λ‹€μ΄λ‚˜λ―Ή' λ³΄λ‹€λŠ” DP, λ‹€μ΄λ‚˜λ―Ή ν”„λ‘œκ·Έλž˜λ°, λ™μ κ³„νšλ²• 정도가 더 μ μ ˆν•  것 κ°™μŠ΅λ‹ˆλ‹€.

Copy link
Contributor

Choose a reason for hiding this comment

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

malloc은 동적 λ©”λͺ¨λ¦¬ 할당에 μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜μΈλ°, MAX 값을 μƒμˆ˜λ‘œ 정해놓고 이λ₯Ό 기반으둜 ν• λ‹Ήν•˜λŠ” 것은 사싀상 정적 λ©”λͺ¨λ¦¬ ν• λ‹Ήκ³Ό λ‹€λ₯Ό λ°” μ—†μ–΄ λ³΄μ΄λ„€μš”..! 정적 λ©”λͺ¨λ¦¬ ν• λ‹ΉμœΌλ‘œλ„ μΆ©λΆ„νžˆ ν•΄κ²°ν•  수 μžˆλŠ” λ¬Έμ œλΌμ„œ λ‹€μŒκ³Ό 같이 μˆ˜μ •ν•˜λ©΄ 훨씬 κ°„λ‹¨ν•œ μ½”λ“œκ°€ 될 것 κ°™μŠ΅λ‹ˆλ‹€.

Suggested change
int* dp = (int*)malloc(sizeof(int) * MAX);
int dp[1000001];

Copy link
Member

@9kyo-hwang 9kyo-hwang May 7, 2025

Choose a reason for hiding this comment

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

μ²¨μ–Έν•˜μžλ©΄, ν•¨μˆ˜ λ‚΄μ—μ„œ 큰 μ‚¬μ΄μ¦ˆμ˜ 배열을 μ„ μ–Έν•˜κ²Œ 될 경우 Stack λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ³ μ •λœ 큰 크기의 배열을 μ„ μ–Έν•˜κ³  μ‹ΆμœΌλ©΄ μ „μ—­ λ³€μˆ˜λ‘œ λ‘λŠ” 게 μ’‹μŠ΅λ‹ˆλ‹€ :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ν™•μ‹€νžˆ κ·Έλ ‡λ„€μš”!
μš”μ¦˜ 무쑰건적으둜 정적 할당을 ν•˜κ³  μžˆλŠ” 데, μ’€ 더 μ½”λ“œλ₯Ό μ“Έ λ•Œ μƒκ°ν•˜κ³ , 상황에 맞게 써봐야 ν•  κ±° κ°™μŠ΄λ‹€!

Comment on lines +15 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

이 μ½”λ“œλŠ” 동적 λ©”λͺ¨λ¦¬ ν• λ‹Ή μ‹€νŒ¨μ— λŒ€λΉ„ν•˜κΈ° μœ„ν•΄μ„œ μž‘μ„±ν•˜μ‹  것 같은데, 이 κ²½μš°μ—λŠ” ν• λ‹Ή 크기가 κ³ μ •λœ μƒμˆ˜(MAX)이고 크기도 비ꡐ적 μž‘μ•„μ„œ 일반적인 ν™˜κ²½μ—μ„œλŠ” ν• λ‹Ή μ‹€νŒ¨κ°€ λ°œμƒν•  κ°€λŠ₯성이 거의 μ—†λ‹€κ³  봐도 λ©λ‹ˆλ‹€. λ°±μ€€μ—μ„œλŠ” μ—₯κ°„ν•΄μ„œ λ©λ‹ˆλ©. μ§€μ›Œλ„ 무방할 것 κ°™λ„€μš”.

Copy link
Member

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

이 λ¬Έμ œλŠ” DP둜 ν’€ 수 μžˆμ§€λ§Œ, κ·Έλž˜ν”„ 문제둜 ν•΄μ„ν•΄μ„œλ„ ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€.
X λ…Έλ“œμ—μ„œ μΆœλ°œν•΄ 1 λ…Έλ“œμ— λ„λ‹¬ν•˜λŠ” "μ΅œλ‹¨ 경둜" μ°ΎκΈ° 문제둜 ν•΄μ„ν•˜λ©΄ λ”± κ·Έλž˜ν”„ 문제 κ°™μ£ ?

#include <iostream>
#include <queue>
#include <unordered_map>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int X; cin >> X;
    queue<int> Q; Q.emplace(X);
    unordered_map<int, int> Map; Map[X] = 0;  // Visited 배열이라 μƒκ°ν•˜λ©΄ λ©λ‹ˆλ‹€.
    
    while(!Q.empty())
    {
        int Current = Q.front(); Q.pop();
        if(Current == 1)
        {
            break;
        }
        else if(Current < 1)
        {
            continue;
        }
        
        if(Current % 3 == 0 && Map.count(Current / 3) == 0)  // 인접 λ…Έλ“œλ‘œ 갈 수 μžˆλŠλƒ
        {
            Map[Current / 3] = Map[Current] + 1;
            Q.emplace(Current / 3);
        }
        
        if(Current % 2 == 0 && Map.count(Current / 2) == 0)  // λ°©λ¬Έν•œ 적이 μ—†λŠ” μ§€ ν•¨κ»˜ 체크
        {
            Map[Current / 2] = Map[Current] + 1;
            Q.emplace(Current / 2);
        }
        
        if(Map.count(Current - 1) == 0)
        {
            Map[Current - 1] = Map[Current] + 1;
            Q.emplace(Current - 1);
        }
    }
    
    cout << Map[1];
    
    return 0;
}

@caucsejunseo
Copy link
Collaborator Author

이 λ¬Έμ œλŠ” DP둜 ν’€ 수 μžˆμ§€λ§Œ, κ·Έλž˜ν”„ 문제둜 ν•΄μ„ν•΄μ„œλ„ ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€. X λ…Έλ“œμ—μ„œ μΆœλ°œν•΄ 1 λ…Έλ“œμ— λ„λ‹¬ν•˜λŠ” "μ΅œλ‹¨ 경둜" μ°ΎκΈ° 문제둜 ν•΄μ„ν•˜λ©΄ λ”± κ·Έλž˜ν”„ 문제 κ°™μ£ ?

이런 생각은 λͺ»ν–ˆλ„€μš”! 아직 λ‹€μ–‘ν•œ 문제λ₯Ό λͺ»ν’€μ–΄ λ΄μ„œ 생각이 κ³ μ •λ˜λŠ” κ±° κ°™μŠ΅λ‹ˆλ‹€...

λ‹€μŒμ—” κ·Έλž˜ν”„ λ¬Έμ œλ„ ν’€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€!

@froglike6
Copy link
Collaborator

μ•žμ„œ dp와 κ·Έλž˜ν”„λ₯Ό μ΄μš©ν•œ 방법을 잘 μ•Œλ €μ£Όμ…”μ„œ μ •μˆ˜λ‘  μͺ½μœΌλ‘œλ„ μƒκ°ν•΄λ΄€λŠ”λ°, -1μ΄λΌλŠ” 쑰건 λ•Œλ¬Έμ— λΆˆκ°€λŠ₯ν•˜λ„€μš”..
κ·Έλž˜μ„œ dpλ₯Ό μ΄μš©ν•΄μ„œ 파이썬으둜 ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

import sys

def solve():
    N = int(sys.stdin.readline())

    if N == 1:
        print(0)
        return

    dp = [0] * (N + 1)

    for i in range(2, N + 1):
        dp[i] = dp[i-1] + 1

        if i % 2 == 0:
            dp[i] = min(dp[i], dp[i//2] + 1)
        
        if i % 3 == 0:
            dp[i] = min(dp[i], dp[i//3] + 1)
            
    print(dp[N])

if __name__ == '__main__':
    solve()

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

이 문제.. μ €λ²ˆμ— ν˜„μ„λ‹˜μ΄ μž¬λ°ŒλŠ” 문제라며 λ˜μ Έμ£Όμ…¨λ‹€κ°€ μ‹€νŒ¨ν•œ λ¬Έμ œλ„€μš©
κ·Έλƒ₯ 그리디 μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ ν’€λ €κ³  ν–ˆλ˜ 것 같은데, dp둜 ν‘ΈλŠ” 게 μ •μ„μ΄μ—ˆκ΅°μš”..!
곡뢀 ν•˜κ³  μ½”λ“œ μ§œλ΄€μŠ΅λ‹ˆλ‹΅

Details

n = int(input())
d = [0] * (n + 1) 

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])

@caucsejunseo caucsejunseo merged commit 450b2b7 into main May 15, 2025
@dohyeondol1 dohyeondol1 deleted the 10-csecaujunseo branch May 24, 2025 14:22
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.

6 participants