개발/알고리즘

위클리 챌린지 - 최소직사각형 (Java, Javascript)

devNam 2021. 11. 18. 23:44
반응형

Java

import java.util.Arrays;
class Solution {
    public int solution(int[][] sizes) {
        int width = 0;
        int height = 0;
        for(int[] size : sizes) {
            Arrays.sort(size);
            width = Math.max(width, size[0]);
            height = Math.max(height, size[1]);
        }
        return width*height;
    }
}

 

Javascript

function solution(sizes) {
    let width = 0;
    let height = 0;
    sizes.map(([w,h]) => w > h ? [w,h] : [h,w])
        .forEach(size => {
            width = Math.max(width, size[0]);
            height = Math.max(height, size[1]);
        });
    return width * height;
}

 

반응형

'개발 > 알고리즘' 카테고리의 다른 글

Programmers - 괄호 회전하기  (0) 2021.12.29
Programmers - 오픈채팅방  (0) 2021.12.25
Programmers - [1차] 다트 게임  (0) 2021.11.19
Programmers - 크레인 인형뽑기 게임  (0) 2021.09.09
Programmers - 위클리 4주차  (0) 2021.09.06