[코딩 테스트 RUN] SWEA 수업 : 풍선팡 보너스 게임

코테런/SWEA(알고리즘 수업)
2026.03.09

 

 

⚠️ 주의!
SWEA에서 낸 코딩 문제에 대한 해답이 들어있습니다.
열람 시 주의해주세요.

 

[SW Expert Academy] 18575. 풍선팡 보너스 게임

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

진짜 쉬운 방법: 누적합

델타 순회를 하지 않아도 누적합으로 풀 수 있다. 혹은 델타순회를 하거나…

가로와 세로의 합을 구하고 자기자신을 뺀 것으로 max와 min 비교를 하면 된다. 자기자신을 빼는 이유: 중복되기 때문. 그리고 마지막에 max-min하면 된다.

 

정답

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

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

        for(int test_case = 1; test_case <= T; test_case++) {
            int N = Integer.parseInt(br.readLine());
            int[][] arr = new int[N][N];
            int[] columnSum =  new int[N];
            int[] rowSum =  new int[N];


            for(int i = 0; i < N; i++){
                StringTokenizer st = new StringTokenizer(br.readLine());
                for (int j = 0; j < N; j++) {
                    arr[i][j] = Integer.parseInt(st.nextToken());
                    //추가로 for문 순회할 필욘 없음.
                    columnSum[j] += arr[i][j];
                    rowSum[i] += arr[i][j];
                }
            }

            int max = rowSum[0] + columnSum[0] - arr[0][0];
            int min = rowSum[0] + columnSum[0] - arr[0][0];

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    int thisSum = rowSum[i] + columnSum[j] - arr[i][j];
                    max = Math.max(max, thisSum);
                    min = Math.min(min, thisSum);
                }
            }

            sb.append("#").append(test_case).append(" ").append(max - min).append("\n");

        }

        System.out.print(sb);
    }
}