프로그래머스 최소직사각형 자바

2023. 5. 7. 15:48코딩테스트/프로그래머스

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

 

프로그래머스

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

programmers.co.kr

 

이 문제의 핵심은 명함을 지갑에 넣을 때 가로로 눕히거나 세로로 눕혀서 넣을 수 있다는 점입니다.

 

1. 하나의 명함의 가로, 세로 중 최대, 최소를 구한다.

2. 최대값 중 최대를 구해주고, 최소값 중 최대를 구해준다.

 

public class Solution86491 {

    public int solution(int[][] sizes) {
        int w_max = 0;
        int h_max = 0;
        int result = 0;

        for (int i = 0; i < sizes.length; i++) {
            int w = Math.max(sizes[i][0], sizes[i][1]);
            int h = Math.min(sizes[i][0], sizes[i][1]);

            w_max = Math.max(w_max, w);
            h_max = Math.max(h_max, h);
        }

        result = w_max * h_max;

        return result;
    }

    public static void main(String[] args) {
        Solution86491 s = new Solution86491();
        int[][] sizes = {{60, 50}, {30, 70}, {60, 30}, {80, 40}};

        int result = s.solution(sizes);

        System.out.println(result);
    }
}