[프로그래머스] Lv. 0 직사각형 넓이 구하기

당근 먹는 쿼카·2023년 3월 20일
0

프로그래머스

목록 보기
67/74
post-thumbnail

🔒 직사각형 넓이 구하기

문제 설명

2차원 좌표 평면에 변이 축과 평행한 직사각형이 있습니다. 직사각형 네 꼭짓점의 좌표 [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]가 담겨있는 배열 dots가 매개변수로 주어질 때, 직사각형의 넓이를 return 하도록 solution 함수를 완성해보세요.

제한 사항

  • dots의 길이 = 4
  • dots의 원소의 길이 = 2
  • -256 < dots[i]의 원소 < 256
  • 잘못된 입력은 주어지지 않습니다.

입출력 예

dotsresult
[[1, 1], [2, 1], [2, 2], [1, 2]]1
[[-1, -1], [1, 1], [1, -1], [-1, 1]]4

🔑 나의 풀이

function solution(dots) {
  let answer = 0;
  const xDots = [dots[0][0], dots[1][0], dots[2][0], dots[3][0]];
  const yDots = [dots[0][1], dots[1][1], dots[2][1], dots[3][1]];
  const width = Math.max(...xDots) - Math.min(...xDots);
  const height = Math.max(...yDots) - Math.min(...yDots);
  answer = width * height;
  return answer;
}

0개의 댓글