프로그래머스 Lv.1 코딩테스트

[프로그래머스 Lv.1] 최소직사각형

하다블 2022. 4. 15. 18:31
반응형

문제는 다음과 같습니다.

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

 

풀이 코드는 다음과 같습니다.

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int solution(vector<vector<int>> sizes)
{
    int max1{0};
    int max2{0};
 
    for (int i{0}; i<sizes.size(); i++)
    {
        max1 = max(max1, max(sizes[i][0], sizes[i][1]));
        max2 = max(max2, min(sizes[i][0], sizes[i][1]));
    }
 
    return max1 * max2;
}

단순하게 가로길이와 세로길이를 구분하여 크기를 비교하게 되면 최소직사각형을 만들 수 없고 최대 크기의 직사각형을 만들게 됩니다. 따라서 가로와 세로를 구분하지 않고 비교를 해야 최소직사각형의 면적을 계산할 수 있습니다.

반응형