알고리즘

[프로그래머스] 최소직사각형(Java) *

muerha 2024. 11. 28. 10:05

 

 

 

 


 

 

나의 풀이

class Solution {
    public int solution(int[][] sizes) {
        int answer = 0;
        int max_height = 0;
        int max_width = 0;
        
        for(int i = 0; i < sizes.length ; i++){
            int max = Math.max(sizes[i][0], sizes[i][1]);
            int min = Math.min(sizes[i][0], sizes[i][1]);
            max_height = Math.max(max_height, max);
            max_width = Math.max(max_width, min);            
        }
        
        return max_height * max_width;
    }
}

 

 

 

다른 사람의 풀이

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