코딩테스트/프로그래머스

[프로그래머스]최소직사각형 - Java

GAEBAL 2022. 9. 2. 22:26
728x90

문제

https://school.programmers.co.kr/learn/courses/30/lessons/86491

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

풀이

레벨 1 문제임

 

애초에 처음부터 긴쪽을 가로, 짧은쪽을 세로로 생각하고 풀면 됨 !

 

 

코드

// 코딩테스트 연습 - 완전탐색 - 최소직사각형
// https://school.programmers.co.kr/learn/courses/30/lessons/86491

package PROGRAMMERS.level1;

public class Num86491_최소직사각형 {
    private static class Solution {
        private int solution(int[][] sizes) {
            int maxW = 0, maxH = 0;
            for (int[] intArr : sizes) {
                int w = Math.max(intArr[0],intArr[1]), h = Math.min(intArr[0],intArr[1]);

                maxW = Math.max(maxW, w);
                maxH = Math.max(maxH, h);
            }

            int answer = maxW*maxH;
            return answer;
        }
    }

    public static void main(String[] args) {
        Solution sol = new Solution();

        System.out.println(sol.solution(new int[][]{{60, 50}, {30, 70}, {60, 30}, {80, 40}}));
        System.out.println(sol.solution(new int[][]{{10, 7}, {12, 3}, {8, 15}, {14, 7}, {5, 15}}));
        System.out.println(sol.solution(new int[][]{{14, 4}, {19, 6}, {6, 16}, {18, 7}, {7, 11}}));
    }
}
728x90