본문 바로가기

프로그래머스/LEVEL 1

[프로그래머스/LEVEL1] 최소직사각형

최소직사각형

 

문제

 

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

 

코딩테스트 연습 - 최소직사각형

[[10, 7], [12, 3], [8, 15], [14, 7], [5, 15]] 120 [[14, 4], [19, 6], [6, 16], [18, 7], [7, 11]] 133

programmers.co.kr

 

코드

class Solution {
    public int solution(int[][] sizes) {
		int result = 0;

		// 가로를 가장 긴부분으로 설정
		int max_w = 0;// 가로
		int max_h = 0;// 세로

		// Math.min([집합]),Math.max([집합])
		for (int i = 0; i < sizes.length; i++) {
			int w = Math.max(sizes[i][0], sizes[i][1]);
			max_w = Math.max(max_w, w);// 80 출력
			int h = Math.min(sizes[i][0], sizes[i][1]);
			max_h = Math.max(max_h, h);// 행별 작은 길이를 추출하고 그중 가장 큰값
		}

		result = max_w * max_h;

		return result;
    }
}