알고리즘/programmers

[Programmers 위클리 챌린지 8주차] 최소직사각형

꾸준함. 2021. 9. 27. 19:56

문제 링크입니다: 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

그리디 하게 두 변 중 긴 변을 가로로 짧은 변을 세로가 되게끔 회전을 시킨 후 (가로길이의 최대 값) * (세로 길이의 최대 값)을 곱해주면 최소 직사각형 면적을 구할 수 있습니다.

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> sizes) {
int width = 0;
int height = 0;
for (auto size : sizes)
{
width = max(width, max(size[0], size[1]));
height = max(height, min(size[0], size[1]));
}
return width * height;
}
int main(void)
{
vector<vector<int>> sizes = {
{60, 50},
{30, 70},
{60, 30},
{80, 40}
};
cout << solution(sizes) << "\n";
return 0;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형