문제
링크
나의 풀이
- 문제 설명의
변이 축과 평행한 직사각형에 힌트를 얻어 풀었다.
- x축과 평행하려면 y축 값이 같아야 함을 이용했다. (y축 평행은 반대)
function solution(dots) {
let x;
let y;
dots.forEach(el => {
dots[0][1] === el[1] ? x = Math.abs(el[0] - dots[0][0]) : null;
dots[0][0] === el[0] ? y = Math.abs(el[1] - dots[0][1]) : null;
});
return (x*y)
}
탐나는 풀이
- 좌표의 성질을 이용하여 문제를 풀었다.
- x축 길이는
max x - min x, y축 길이는좌표는 max y = min y로 구할 수 있다.
function solution(dots) {
let x = [],
y = [];
for (let pos of dots) {
x.push(pos[0]);
y.push(pos[1]);
}
return (Math.max(...x) - Math.min(...x)) * (Math.max(...y) - Math.min(...y))
}