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

well-life-gm·2021년 11월 2일
0

프로그래머스

목록 보기
17/125

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

처음에는 주어진 점들에 대해서 (a, b), (b, a)에 index를 기록해놓고, 모든 index를 다 포함하는 최소 직사각형을 찾으려했다.
이렇게 구현하다보니 굳이 직접 맵에 기록하고, 일일이 직사각형을 쳐서 구하지 않아도 아래와 같이 코드를 작성하면 된다는 것을 깨달았다.

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> sizes) {
    int answer = 0;
    int max1 = 0, max2 = 0;
    for(int i=0;i<sizes.size();i++) {
        if(sizes[i][0] > sizes[i][1]) {
            max1 = max(sizes[i][1], max1);
            max2 = max(sizes[i][0], max2);
        } else {
            max1 = max(sizes[i][0], max1);
            max2 = max(sizes[i][1], max2);
        }
    }
    answer = max1 * max2;
    return answer;
}
profile
내가 보려고 만든 블로그

0개의 댓글