Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2274}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: 그리디 + 다이나믹프로그래밍
  • 🔹 어떤 방식으로 접근했는지

우선 자릿수를 늘리기 위하여 가장 비용이 적게 드는 것으로 채워둔다.
그 후, 남음 금액에 대하여 맨 앞자리부터 큰 수로 채울 수 있는지 확인하여 채울 수 있다면 치환하는 식으로 진행한다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N)
  • 이유:

가장 저렴한 금액이 1원이라고 가정하고,
N자리에 대해 M원 주어졌다면, M번 반복하므로

💻 구현 코드

#include <iostream>
#include <queue>
#include <string>
 
#define kit ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
using namespace std;
typedef pair<int,int> pii;
 
priority_queue<pii, vector<pii>, greater<pii>> mi;
int n, m, p[50], c[50], cnt=0;
string ans = "";
 
int main() {
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> p[i];
        mi.push({p[i], i});
    }
    cin >> m;
    
    pii cur = mi.top(); // 가장 싼방번호 저장
    if (cur.second == 0) {
        mi.pop();
        if (mi.top().first > m) { //구매할 수 없을 때 0
            cout << 0;
            return 0;
        }
    }
    
    m -= mi.top().first; c[cnt] = mi.top().first;
    ans += to_string(mi.top().second);
    cnt++;
    
    while (m >= cur.first) { // 제일 싼 거로 남은 부분 모두 채워줌
        m -= cur.first;
        ans += to_string(cur.second);
        c[cnt] = cur.first; cnt++;
    }
 
    int j=0;
    // 앞에서부터 큰 수로 바꿀 수 있는지 확인해줌
    // 더이상 구매할 수 없을 때, 범위를 나갔을 때,
    while(j < ans.length())
    {
        for (int i=n-1; i>0; i--)
        {
            // 구매할 필요가 없는 경우
            if ((int)ans[j] - '0' >= i) break;
            if (m + c[j] >= p[i]) { // 갱신할 돈을 구매할 수 있으면 구매함
                ans[j] = (char)(i + 48);
                m = m - (p[i] - c[j]);
                c[j] = p[i];
                break;
            }
        }
        j++;
    }
    
    for (char a : ans) cout << a;
}

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.

260106 : 코딩테스트

2 participants