Skip to content

Conversation

@sksn12
Copy link
Contributor

@sksn12 sksn12 commented Jan 5, 2026

🚀 이슈 번호

Resolve: {#2270}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지 : DP
  • 🔹 어떤 방식으로 접근했는지 : 밑면 넓이를 내림차순으로 정렬한 뒤, 무게가 더 무거운 벽돌 위에만 올릴 수 있다는 조건으로 DP를 돌려 높이 합의 최댓값을 구하고 역추적

⏱️ 시간 복잡도

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

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

💻 구현 코드

import java.io.*;
import java.util.*;

public class Main {

    static class Brick {
        int area, height, weight, idx;
        Brick(int area, int height, int weight, int idx) {
            this.area = area;
            this.height = height;
            this.weight = weight;
            this.idx = idx;
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        Brick[] bricks = new Brick[N];
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int area = Integer.parseInt(st.nextToken());
            int height = Integer.parseInt(st.nextToken());
            int weight = Integer.parseInt(st.nextToken());
            bricks[i] = new Brick(area, height, weight, i + 1);
        }

        // 1. 밑면 넓이 내림차순 정렬
        Arrays.sort(bricks, (a, b) -> b.area - a.area);

        int[] dp = new int[N];
        int[] prev = new int[N];
        Arrays.fill(prev, -1);

        int maxHeight = 0;
        int lastIdx = 0;

        // 2. DP
        for (int i = 0; i < N; i++) {
            dp[i] = bricks[i].height;
            for (int j = 0; j < i; j++) {
                if (bricks[j].weight > bricks[i].weight) {
                    if (dp[j] + bricks[i].height > dp[i]) {
                        dp[i] = dp[j] + bricks[i].height;
                        prev[i] = j;
                    }
                }
            }
            if (dp[i] > maxHeight) {
                maxHeight = dp[i];
                lastIdx = i;
            }
        }

        // 3. 역추적
        List<Integer> result = new ArrayList<>();
        while (lastIdx != -1) {
            result.add(bricks[lastIdx].idx);
            lastIdx = prev[lastIdx];
        }

        // 출력
        System.out.println(result.size());
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }
}

@sksn12 sksn12 self-assigned this Jan 5, 2026
@sksn12 sksn12 requested a review from Eunjin3395 as a code owner January 5, 2026 15:27
@sksn12 sksn12 linked an issue Jan 5, 2026 that may be closed by this pull request
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.

260105 : 코딩테스트

2 participants