Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 28, 2026

🚀 이슈 번호

Resolve: {#2337}

🧩 문제 해결

스스로 해결: ✅ / ❌

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지 DP, 재귀
  • 🔹 어떤 방식으로 접근했는지
    dp(start, end) = max(dp(start, i) + dp(i+1, end) + (두덩어리를 합치는데 드는 비용)) (start <= i < end) 으로 보고
    top down 방식으로 dp를 구현

⏱️ 시간 복잡도

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

  • Big-O 표기법: O(N^3)
  • 이유:
    채워야하는 칸의 개수 = N^2
    채우는데 드는 비용 = N

💻 구현 코드

import java.io.*;

public class Main {

  static int N;
  static int[][] nums, dp;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] temp;

    N = Integer.parseInt(br.readLine());
    nums = new int[N][2];
    dp = new int[N][N];

    // 초기값
    for (int n = 0; n < N; n++) {
      temp = br.readLine().split(" ");
      nums[n][0] = Integer.parseInt(temp[0]);
      nums[n][1] = Integer.parseInt(temp[1]);
      for (int m = 0; m < N; m++) {
        dp[n][m] = -1;
      }
    }
    System.out.println(findMax(0,N-1));
  }

  static int findMax(int si, int ei) {
    if (dp[si][ei] != -1) return dp[si][ei];

    dp[si][ei] = Integer.MAX_VALUE;
    if (si == ei) dp[si][ei] = 0;
    else if (si+1 == ei) dp[si][ei] = nums[si][0] * nums[si][1] * nums[ei][1];
    else {
      for (int i = si; i < ei; i++) {
        dp[si][ei] = Math.min(
            findMax(si, i) + findMax(i + 1, ei) + (nums[si][0] * nums[i][1] * nums[ei][1]),
            dp[si][ei]);
      }
    }

    return dp[si][ei];
  }
}

@sunha20 sunha20 self-assigned this Jan 28, 2026
@sunha20 sunha20 linked an issue Jan 28, 2026 that may be closed by this pull request
@sunha20 sunha20 merged commit a7f4aae into main Jan 30, 2026
1 check passed
@sunha20 sunha20 deleted the sunha/2337/1 branch January 30, 2026 10:42
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.

260128 : 코딩테스트

2 participants