코딩테스트/Programmers

[Programmers] 최소직사각형 (완전탐색)

주니어주니 2023. 8. 28. 18:20

 

 

 

 

 

내 풀이 

import java.util.*;

class Solution {
    public int solution(int[][] sizes) {
        int maxW = 0;
        int maxH = 0;

        for(int i=0; i<sizes.length; i++) {
            int w = sizes[i][0];
            int h = sizes[i][1];

            if (w < h) {
                int temp = w; 
                w = h; 
                h = temp;
            }
            maxW = Math.max(maxW, w); 
            maxH = Math.max(maxH, h);

        }

        return maxW*maxH;
    }
}

 

 

 

다른 사람 풀이 

import java.util.*;

class Solution {
    public int solution(int[][] sizes) {
        int width = 0; 
        int height = 0; 
        
        for(int[] arr : sizes) {
            width = Math.max(width, Math.max(arr[0], arr[1])); 
            height = Math.max(height, Math.min(arr[0], arr[1]));
        }
        
        return width*height;
    }
}