LeetCode - 1725. Number Of Rectangles That Can Form The Largest Square

henu·2023년 10월 5일
0

LeetCode

목록 보기
101/186

Solution

var countGoodRectangles = function(rectangles) {
    const squares = rectangles.map(e => Math.min(...e))

    return squares.filter(e => e === Math.max(...squares)).length
};

Explanation

문제 설명글의 길이에 비해 생각보다 간단한 문제였다.
어떤 직사각형을 잘라서 최대 크기의 정사각형을 만들려면 두 변 중 작은 길이의 변을 갖는 정사각형으로 만들면된다. (Math.min 메소드 이용)
그리고 여러 정사각형들 중 가장 큰 정사각형과 크기가 같은 것들을 걸러내서 개수를 구하면 된다. (Math.max 메소드 이용)

0개의 댓글