최소 직사각형 - 완전탐색

정태수·2025년 3월 27일

앞으로 if는 왠만하면 안쓰는게 나을 것 같다.

// 옛날꺼 기록용
// function solution(sizes) {
//     var answer = 0;
//     //가장 큰 수가 가로인지 세로인지
//     //만약 가장 큰 수가 가로 라면, 세로에서 가장 큰 수의 가로는 다른 세로들보다 큰지 만약 더크면 안됨. 
//     // 작다면 그 다음 값이 작은 세로값이랑 곱한것이 최소 넓이.
//     // 
//     let maxWidth = 0;
//     let maxHeight = 0;
//     let maxHeightIndex;
//     let maxWidthIndex;
//     let secondMaxWidth = 0;
//     let secondMaxHeight = 0;
//     for(let i = 0; i < sizes.length; i++) {
//         if(maxWidth < sizes[i][0]) {
//             maxWidthIndex = i;
//             maxWidth = sizes[i][0];
//         }
//         if(maxHeight < sizes[i][1]) {
//             //만약 같은 값 height...
//             maxHeightIndex = i;
//             maxHeight = sizes[i][1];
//         }
//         // console.log("maxWidth:", maxWidth, "maxHeight:", maxHeight)
//         //가장 큰 수가 있는 곳은 픽스를 하고 아닌 곳의 가장 큰수의 가로또는 세로
//         //한마디로 세컨드 숫자를 찾으면 된다?
//     }
//     const biggestArea = maxWidth * maxHeight;
//     // console.log("biggestArea:", biggestArea)
    
    
    
// //     //예제2번) 가장 큰 사이즈 구하고 다른 것들 돌려보면서 다 들어가는지도 확인.
// //     if(maxWidth>maxHeight) {
// //         let biggestPair = sizes[maxWidthIndex];
// //         console.log('biggestPair:', biggestPair)
        
// //         sizes.splice(maxWidthIndex, 1)
// //         let afterDelete = sizes;
        
        
// //     } else if (maxWidth < maxHeight) {
// //         let biggestPair = sizes[maxHeightIndex];
// //         console.log('biggestPair:', biggestPair)
// //         sizes.splice(maxHeightIndex, 1)
// //         let afterDelete = sizes;
// //         console.log('afterDelete:', afterDelete)
        
// //         for (let i = 0; i < afterDelete.length; i++) {
// //             //biggestPair - [바꾼 배열] < 0 으면 그건 못바꾼다는 것.
// //         }
        
// //     }
    
    
    
    
    
//     if(maxWidth>maxHeight) {
//         console.log('역적이다', sizes[maxHeightIndex])
        
//         // console.log("sizes:", sizes.splice(maxHeightIndex, 1, sizes[maxHeightIndex].reverse()))
//         sizes.splice(maxHeightIndex, 1, sizes[maxHeightIndex].reverse())
//         let afterDelete = sizes;
//         console.log('afterDelete:', afterDelete)

//         //바꿨는데 제일 크다면 
        
//     for(let j = 0; j < afterDelete.length; j++) {
//         if(secondMaxWidth < afterDelete[j][0]) {
//             secondMaxWidth = afterDelete[j][0];
//         }
//         if(secondMaxHeight < afterDelete[j][1]) {
//             //만약 같은 값 height...
//             secondMaxHeight = afterDelete[j][1];
//         }
//     }
//         const secondBiggestArea = secondMaxWidth * secondMaxHeight;
//         //크지 않다면?
//         answer = Math.min(secondBiggestArea, biggestArea)
        
//     } else if(maxWidth < maxHeight) {
        
//         console.log('역적이다 height', sizes[maxWidthIndex])
//         sizes.splice(maxWidthIndex, 1, sizes[maxWidthIndex].reverse())
//         let afterDelete = sizes;
//         console.log('maxWidthIndex:', maxWidthIndex)
//         console.log("afterDelete:", afterDelete)
        
//             for(let j = 0; j < afterDelete.length; j++) {
//             if(secondMaxWidth < afterDelete[j][0]) {
//                 secondMaxWidth = afterDelete[j][0];
//             }
//             if(secondMaxHeight < afterDelete[j][1]) {
//                 //만약 같은 값 height...
//                 secondMaxHeight = afterDelete[j][1];
//             }
//         }
        
//         const secondBiggestArea = secondMaxWidth * secondMaxHeight;
//         answer = Math.min(secondBiggestArea, biggestArea)
//     }
    
//     return answer;
// }

완전 탐색이라고해서 꼭 for문 써야하는것 아닌것 같다.
문제를 보면서 어떻게 간단히 하는지가 훠얼씬 중요한것 같다.
요지는 넓이를 구하는것. 어떤 넓이냐면 모든 수치에 대해서 들어갈수 있는 너비
(여기서 좀 꼬인것 같다 if 를 써서 해결해주려고 했으니... )
문제를 보는 시야를 if말고 메소드를 통해서 해결하고자 해야겠다.
사실 메소드에 대한 이해도가 낮기에 계속 뻉뺑돌리려는거 아닌가 싶다

function solution(sizes) {
    var answer = 0;
    
    let w = [];
    let h = [];
    // 이차원 배열의 큰 값과 작은 값을 구분해서 큰 값은 w, 작은 값은 h에 넣어줍니다.
    sizes.map((v,i) => {

        w[i] = Math.max(...v)

        h[i] = Math.min(...v)

    })
    
    // console.log(w, h)
   answer  = (Math.max(...w) * Math.min(...h))

    return (Math.max(...w) * Math.max(...h))
    
    // return answer;
}

sizes.map()으로 모든 배열에 대한 처리를 할수 있는데 이때 너비, 높이를 비교해서 높은 쪽을 무조건 너비라 생각한다. 이렇게 생각하면 굳이 if 안해도 됐음.

둘이 비교해서 큰건 큰쪽으로 작은건 작은 배열쪽으로 이동해버리게 된다.

그리고 문제에 맞는 큰값중 가장 큰값 * 작은 값들중 작은값 답이 완성된다....

profile
프론트엔드 개발자

0개의 댓글