undefined
[프로그래머스] 최소 직사각형 -JS 본문
반응형
문제 설명
문제 풀이
function solution(sizes) {
const rotated = sizes.map(([w,h])=> w > h ? [w,h] : [h,w])
let wArr = []
let hArr = []
rotated.map(([w,h]) => {
wArr.push(w)
hArr.push(h)
})
return Math.max(...wArr) * Math.max(...hArr)
}
1. 배열을 맵핑하여 w,h를 비교, 각자 큰 쪽으로 큰 수 몰아주기
2. 배열에 각각 넣어준다
3. 가장 큰 수를 찾아서 곱해준다.
*다른 분의 코드를 참고하였습니다.
개선 사항
function solution(sizes) {
const rotated = sizes.map(([w,h])=> w > h ? [w,h] : [h,w])
let maxVal = [0,0]
rotated.map(([w,h]) => {
if(w > maxVal[0]) maxVal[0]=w
if(h > maxVal[1]) maxVal[1]=h
})
return maxVal[0]*maxVal[1]
}
배열에 넣고 비교하는 것보다 숫자를 활용하여 크기비교를 해주면 훨씬 빠르다.
반응형
'Coding Test' 카테고리의 다른 글
[프로그래머스] 내적 - JS (0) | 2022.06.23 |
---|---|
[프로그래머스] 키패드 누르기 -JS (0) | 2022.06.22 |
[프로그래머스] 폰켓몬 -JS (0) | 2022.06.20 |
[프로그래머스] 체육복 -JS (0) | 2022.06.19 |
[프로그래머스] 모의고사 -JS (0) | 2022.06.11 |
Comments