최대 1 분 소요

Question

Q Q2


Solution

  • 한 쪽 길이에 가장 큰 숫자를 몰아주고 최대 길이 끼리 곱하면 지갑의 크기가 결정된다.

  • 추가: max() 함수를 이용하면 코드를 간략화 할 수 있다.


Cord

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> sizes) 
{
    int answer = 0;
    int maxW = 0;
    int maxH = 0;
    
    // 큰 숫자는 가로, 작은 숫자는 세로로 정렬하기
    for(int i = 0; i < sizes.size(); i++)
    {
        if (sizes[i][0] < sizes[i][1])
        {
            int temp = sizes[i][0];
            sizes[i][0] = sizes[i][1];
            sizes[i][1] = temp;
        }
    }
    
    // 각각의 가장 큰 가로와 세로 길이 뽑기
    for(int i = 0; i < sizes.size(); i++)
    {
        if (maxW < sizes[i][0]) { maxW = sizes[i][0]; }
        if (maxH < sizes[i][1]) { maxH = sizes[i][1]; }
    }
    
    // 최종 지갑의 크기
    answer = maxW * maxH;
    
    return answer;
}

Result

Result