프로그래머스_C#/Level_1

[프로그래머스 C#] 8주차_최소직사각형

최애뎡 2021. 10. 21. 00:05
728x90
반응형

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

using System;

public class Solution {
    public int solution(int[,] sizes) {
        int answer = OptimalSize(sizes);        
        return answer;
    }
    
    int OptimalSize(int[,] sizes)
    {
        int maxW = 0, maxH = 0;
        for (int i = -1; ++i < sizes.GetLength(0);)
        {
            int temp_w = sizes[i, 0];
            int temp_h = sizes[i, 1];
            
            if (temp_h > temp_w)
            {
                sizes[i, 0] = temp_h;
                sizes[i, 1] = temp_w;
            }
            
            if (sizes[i, 0] > maxW) maxW = sizes[i, 0];
            if (sizes[i, 1] > maxH) maxH = sizes[i, 1];
        }
        
        return maxW * maxH;
    }
}

명함을 돌릴 수 있으니까 긴 부분을 가로로 두고 짧은 부분을 세로로 한 뒤 가장 긴 가로와 가장 긴 세로의 곱을 반환

반응형