devlog_owen
[프로그래머스] 최소직사각형 본문
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
'algorithm > (js)프로그래머스' 카테고리의 다른 글
[프로그래머스] 크기가 작은 부분문자열 (0) | 2023.11.06 |
---|---|
[프로그래머스] 이상한 문자 만들기 (0) | 2023.11.05 |
[프로그래머스] 삼총사 (0) | 2023.11.05 |
[프로그래머스] 3진법 뒤집기 (0) | 2023.11.05 |
[프로그래머스] 예산 (0) | 2023.11.05 |