250x250
Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

devlog_owen

[프로그래머스] 최소직사각형 본문

algorithm/(js)프로그래머스

[프로그래머스] 최소직사각형

developer_owen 2023. 11. 5. 19:40
728x90

문제

 

1. 각 배열의 값 중 큰 값을 왼쪽에 옮김

2. 배열별로 왼쪽, 오른쪽 값들끼리 묶어서 큰값을 새 배열에 넣음

3. 왼,오 값 곱해서 넓이 도출


 

 

나의 풀이

function solution(sizes) {
    let maxFirstIndex = 0;
    let maxSecondIndex = 0;
    
    for(let i = 0; i<sizes.length; i++){
        const currentsize = sizes[i]
        
        if( currentsize[0] <currentsize[1]){
            [currentsize[0],currentsize[1]] =[currentsize[1],currentsize[0]]
        }
    maxFirstIndex = Math.max(...sizes.map((array) => array[0]));
        maxSecondIndex = Math.max(...sizes.map((array) => array[1]));
    }
    
    return maxFirstIndex*maxSecondIndex;
}

1. 각 배열의 값 중 큰 값을 왼쪽에 옮김

if( currentsize[0] <currentsize[1]){
            [currentsize[0],currentsize[1]] =[currentsize[1],currentsize[0]]
        }

 

조건문으로 더 큰 값을 왼쪽에 옮김

 

2. 배열별로 왼쪽, 오른쪽 값들끼리 묶어서 큰값을 새 배열에 넣음

  maxFirstIndex = Math.max(...sizes.map((array) => array[0]));
        maxSecondIndex = Math.max(...sizes.map((array) => array[1]));

 

map함수를 이용해서 0,1번째 인덱스들을 뽑아내고 Math.max로 그 중에서 최댓값을 뽑아냄.

 

3. 왼,오 값 곱해서 넓이 도출

 return maxFirstIndex*maxSecondIndex;

 

 

 


 

다른 사람 풀이

function solution(sizes) {
    const rotated = sizes.map(([w, h]) => w < h ? [h, w] : [w, h]);

    let maxSize = [0, 0];
    rotated.forEach(([w, h]) => {
        if (w > maxSize[0]) maxSize[0] = w;
        if (h > maxSize[1]) maxSize[1] = h;
    })
    return maxSize[0]*maxSize[1];
}

 

 

 


 

회고

 

본문1


 

 

 

 

728x90