최소직사각형

문제


문제 풀이

문제자체가 좀 길어서 해석하기가 어려워 보일 수 있으나 단순히 가로의 큰값 * 세로의 큰값을 곱해주면 되는 간단한 문제이다. 

 


나의 답안

public static int solution(int[][] sizes) {
    int widthMax = Integer.MIN_VALUE;
    int heightMax = Integer.MIN_VALUE;

    for (int[] arr : sizes) {
        if (arr[0] < arr[1]) {
            int tmp = arr[0];
            arr[0] = arr[1];
            arr[1] = tmp;
        }

        if (widthMax < arr[0]) widthMax = arr[0];
        if (heightMax < arr[1]) heightMax = arr[1];
    }

    return widthMax * heightMax;
}

다른 답안

변수를 초기화 방식에서 Math 클래스를 사용하여 처리하는 방식이다. Math 클래스는 자주 사용이 되니 참고 하도록 하자.

class Solution {
    public int solution(int[][] sizes) {
        int length = 0, height = 0;
        for (int[] card : sizes) {
            length = Math.max(length, Math.max(card[0], card[1]));
            height = Math.max(height, Math.min(card[0], card[1]));
        }
        int answer = length * height;
        return answer;
    }
}

'Programmers 문제풀이 > Lv.1' 카테고리의 다른 글

문자열 내 마음대로 정렬하기  (0) 2023.03.18
[1차] 비밀지도  (0) 2023.03.18
시저 암호  (0) 2023.03.17
예산  (0) 2023.03.16
3진법 뒤집기  (0) 2023.03.16