이번 알고리즘은 문제부터 어려워서 다른분들 정리한거 읽으면서 이해했따능>< 데헷큥! 구글링을 하다보면 천재님들이 참 많다! 쌍따봉과 박수갈채를 보냅니다👏🏻👏🏻
function solution(sizes) { let w = [] let h = [] for(let i=0; i<sizes.length; i++){ let max = Math.max(sizes[i][0],sizes[i][1]) let min = Math.min(sizes[i][0],sizes[i][1]) w.push(max) h.push(min) } return Math.max(...w) * Math.max(...h) }
sizes
의 길이만큼 도는 for문을 작성sizes
의 최대, 최소일 때 값들을 구하고, 가로와 세로를 나타내는 배열에 push를 해준다Math.max
를 통해 가로와 세로의 최댓값을 곱해준다function solution(sizes) { let w = 0; let h = 0; sizes.forEach(s => { const [a, b] = s.sort((a,b) => a-b); if (a > h) h = a; if (b > w) w = b; }); return w * h; }
function solution(sizes) { const newSizes = sizes.map(e => e.sort((a, b) => a - b)); return Math.max(...newSizes.map(e => e[0])) * Math.max(...newSizes.map(e => e[1])); }