Hseong

[프로그래머스 고득점 kit - 완전탐색] - 최소직사각형 본문

Algorithm/Java

[프로그래머스 고득점 kit - 완전탐색] - 최소직사각형

__hseong__ 2025. 4. 29. 22:41
728x90
반응형

import java.util.Arrays;

class Solution {
    public int solution(int[][] sizes) {
        int answer = 0;
        
        int[] width_li = new int[sizes.length];
        int[] height_li = new int[sizes.length];
        
        for (int i = 0; i < sizes.length; i++) {
            int width = Math.max(sizes[i][0], sizes[i][1]);
            int height = Math.min(sizes[i][0], sizes[i][1]);
            
            width_li[i] = width;
            height_li[i] = height;
        }
        
        Arrays.sort(width_li);
        Arrays.sort(height_li);
        
        answer = width_li[width_li.length - 1] * height_li[height_li.length - 1];
        
        return answer;
    }
}
  • 해당 문제는 n 개의 가로 + 세로 가 주어진 명함 리스트가 주어지면 가로세로 회전을 하면서 최소한으로 다 덮을 수 있는 max 크기를 구해서 최종 넓이로 출력하는 것이 핵심이다.
  • 일단 각 명함마다 가로/세로를 담을 정수형 배열을 선언하고, 가로-세로를 비교하여 큰 것은 가로, 작은 것은 세로에 담는다 
  • 그 후 Arrays.sort() 를 통해 각각 정렬하고 , 마지막 인덱스는 각 요소의 최대값이기 때문에 이를 곱하여 출력하면 문제의 답이 된다!!
728x90
반응형