GitHubSeob

C++ / 프로그래머스 / 위클리 챌린지 8주차 / 최소직사각형 본문

Programmers/Level 1

C++ / 프로그래머스 / 위클리 챌린지 8주차 / 최소직사각형

GitHubSeob 2021. 10. 15.

문제

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

 

코딩테스트 연습 - 8주차_최소직사각형

[[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

문제풀이

sizes를 탐색하면서 [idx1][0]인 부분을 더 큰 수, [idx1][1]인 부분을 더 작은 수로 바꾼다.

[idx1][0]끼리, [idx1][1]끼리 max_length값을 계속 경신한다.

둘을 곱한 값을 return 한다.

 

코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<vector<int>> sizes) {
    int idx1(0);
    int max_length_w(0);
    int max_length_h(0);
    for (idx1 = 0; idx1 < sizes.size(); ++idx1) {
        if (sizes[idx1][0] < sizes[idx1][1])
            swap(sizes[idx1][0], sizes[idx1][1]);
        max_length_w = max(max_length_w, sizes[idx1][0]);
        max_length_h = max(max_length_h, sizes[idx1][1]);
    }

    return max_length_w * max_length_h;
}