Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 19, 2026

🚀 이슈 번호

Resolve: {#2314}

🧩 문제 해결

스스로 해결: ✅ / ❌

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지 그리디
  • 🔹 어떤 방식으로 접근했는지
    랭킹 차이가 더 적은 선수와 경기를 치르는게 비용이 가장 적게 듬
    리스트에서 최댓값을 찾고, 양옆의 이웃과 비교하여 차이를 결과값에 더한 뒤 해당 최댓값을 리스트에서 제거하는 과정을 반복

⏱️ 시간 복잡도

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

  • Big-O 표기법: O(N^2)
  • 이유:
    매 루프마다 리스트에서 최댓값을 찾기 위해 N개를 탐색
    이 과정을 N번 반복

💻 구현 코드

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        
        int totalDiff = 0;
        
        while (list.size() > 1) {
            int maxIdx = 0;
            int maxVal = list.get(0);
            
            for (int i = 1; i < list.size(); i++) {
                if (list.get(i) > maxVal) {
                    maxVal = list.get(i);
                    maxIdx = i;
                }
            }
            
            int diff = Integer.MAX_VALUE;
            
            if (maxIdx > 0) {
                diff = Math.min(diff, maxVal - list.get(maxIdx - 1));
            }
            
            if (maxIdx < list.size() - 1) {
                diff = Math.min(diff, maxVal - list.get(maxIdx + 1));
            }
            
            totalDiff += diff;
            list.remove(maxIdx);
        }
        
        System.out.println(totalDiff);
    }
}

@sunha20 sunha20 self-assigned this Jan 19, 2026
@sunha20 sunha20 requested a review from dlchdaud123 as a code owner January 19, 2026 15:45
@sunha20 sunha20 linked an issue Jan 19, 2026 that may be closed by this pull request
@sunha20 sunha20 merged commit eaaab3a into main Jan 30, 2026
1 check passed
@sunha20 sunha20 deleted the sunha/2314/2 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.

260119 : 코딩테스트

2 participants