문제 : https://programmers.co.kr/learn/courses/30/lessons/86491
function solution(arr) {
const num1 = [];
const num2 = [];
for (let i = 0; i < arr.length; i += 1) {
// arr[i][0]와 arr[i][1]를 비교해서, 큰 수를 num1에 넣기
if (arr[i][0] > arr[i][1]) {
num1.push(arr[i][0]);
num2.push(arr[i][1]);
} else {
num1.push(arr[i][1]);
num2.push(arr[i][0]);
}
}
return Math.max(...num1) * Math.max(...num2);
}
// 실행코드
console.log(solution([[60, 50],[30, 70],[60, 30],[80, 40],]),); // 4000
console.log(solution([[10, 7],[12, 3],[8, 15],[14, 7],[5, 15],]),); // 120
console.log(solution([[14, 4],[19, 6],[6, 16],[18, 7],[7, 11],]),); // 133
function solution(arr) {
const [x, y] = arr.reduce(
(acc, cur) => {
return [
Math.max(acc[0], Math.max(...cur)),
Math.max(acc[1], Math.min(...cur)),
];
},
[0, 0],
);
return x * y;
}
function solution(arr) {
// 1.이중배열 구조를 유지한 채, 안에있는 숫자를 오름차순으로 정렬함
const newArr = arr.map(([a, b]) => [Math.max(a, b), Math.min(a, b)]);
// 2.이중배열을 돌면서 인덱스 0,1별로 젤 큰수를 maxSize배열에 담음
const maxSize = [0, 0];
newArr.forEach((el) => {
maxSize[0] = Math.max(maxSize[0], el[0]);
maxSize[1] = Math.max(maxSize[1], el[1]);
});
// 3.maxSize배열의 각 수를 곱하여 반환.
return maxSize[0] * maxSize[1];
}