Bamboo is coming

[211101] 프로그래머스 위클리챌린지 최소직사각형 문제풀이 java 본문

PS

[211101] 프로그래머스 위클리챌린지 최소직사각형 문제풀이 java

twenty 2022. 1. 12. 15:07

 

 


 

 

class Solution {
    public int solution(int[][] sizes) {
        int answer = 0;
        
        int max_h, max_w;
        int tmp;

        for(int i=0;i<sizes.length;i++){
            if(sizes[i][0]<sizes[i][1]){
                tmp = 0;
                tmp = sizes[i][0];
                sizes[i][0] = sizes[i][1];
                sizes[i][1] = tmp;
            }
        }

        max_w = sizes[0][0];
        max_h = sizes[0][1];

        for(int i=1;i<sizes.length;i++){
            if(max_w<sizes[i][0])
                max_w = sizes[i][0];
            if(max_h<sizes[i][1])
                max_h = sizes[i][1];
        }

        answer=max_h*max_w;
        
        return answer;
    }
}
 

 

 

 

먼저 순차 비교를 생각해봤는데 역시 그거까지 고려했는지 입출력 예시 3번에 완전 다 섞어서 조합해야하는 예시를 들었다.

가로 < 최대 세로, 세로 < 최대 가로면 성립하니까 어쨌든 최대값이랑만 맞으면 전부 수납가능하다.

그래서 가로 세로 중에 최대값을 비교해서 0열에 정렬해서 풀었다.

 

 


 

 

 

 

Comments