Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 6, 2026

🚀 이슈 번호

Resolve: {#2274}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지 그리디, (DP)
  • 🔹 어떤 방식으로 접근했는지
    단순 dp로만 구현했는데 틀려서 찾아보니 그리디로 풀면 보다 단순했다.
    비용이 가장 적은 숫자를 최대한 많이 사고 그렇게 만들어진 수를 가져와서 맨 앞자리부터 맨 끝자리까지 최대한 큰 수로 교체해야 하면 된다.
    dp로도 풀수 있을 것 같긴하다

⏱️ 시간 복잡도

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

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

💻 구현 코드

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class number{
	int num, price;
	number(int num, int price) {this.num = num; this.price = price;}
}

public class BOJ_1082_방번호 {
	private static int N, pocket;
	private static int res[] = new int[100];
	private static number arr[];
	private static Map<Integer, Integer> m = new HashMap<>(); // 숫자-비용 따로 저장해두려고
	
	public static void main(String[ ] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt(); arr = new number[N];
		for(int i = 0; i < N; i++) {
			arr[i] = new number(i, sc.nextInt());
			m.put(i, arr[i].price);
		}
		pocket = sc.nextInt();
		
		Arrays.sort(arr, new Comparator<number>() { // 가격 기준 오름차순 정렬
			@Override
			public int compare(number o1, number o2) {
				if(o1.price == o2.price) return o1.num - o2.num;
				return o1.price - o2.price;
			}
			
		});
		
		int cnt = 0;
		if(arr[0].num == 0) { // 숫자 0이 가장 작은 수라서 맨 앞에 올 수 없기 때문에
			if(N == 1 || arr[1].price > pocket) { // 0대신 넣을 그 다음 숫자의 비용을 감당할 수 없다면
				System.out.println(0); // 숫자 0으로밖에 할 수 없을 때 답은 0
				System.exit(0);
			}
			res[cnt++] = arr[1].num; // 그 다음 숫자의 비용을 감당할 수 있으면 이 숫자로 맨 앞자리 채운다
			pocket -= arr[1].price;
		}
		
		while(pocket - arr[0].price >= 0) { // 가장 비용이 작은 숫자로 최대한 많이 채움
			res[cnt++] = arr[0].num;
			pocket -= arr[0].price;
		}
		
		for(int i = 0; i < cnt; i++) {
			for(int j = N - 1; j >= 0; j--) { // 가장 큰 수부터
				if(i == 0 && j == 0) continue; // 맨 앞자리에 0이 들어가면 안 됨
				int tmp = pocket + m.get(res[i]) - m.get(j);
				if(tmp >= 0) { // 가격 범위 안 넘어서 현재 숫자로 바꿀 수 있으면 바꿈(최대한 큰 수이므로)
					pocket = tmp; // 허용 가능한 비용 갱신
					res[i] = j;
					break;
				}	
			}
		}
		
		for(int i = 0; i < cnt; i++) System.out.print(res[i]);
	}
}

@sunha20 sunha20 linked an issue Jan 6, 2026 that may be closed by this pull request
@sunha20 sunha20 requested a review from dlchdaud123 as a code owner January 6, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260106 : 코딩테스트

2 participants