본문 바로가기
개발로그/알고리즘

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

by 쩜징 2023. 7. 30.

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


2차원 좌표 평면에 변이 축과 평핸한 직사각형이 있다.

직사각형의 네 꼭짓점의 좌표가

[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

dots배열에 담겨 있다.

직사각형의 넓이를 return하라.

 

dots result
[[1, 1], [2, 1], [2, 2], [1,2]] 1
[[-1, 1], [1, 1], [1, -1], [-1, 1]] 4

 

** 풀이 방법

 

x축과 y축 길이를 구해야 한다.

x = x축 최대 길이 - x축 최소 길이

y = y축 최대 길이 - y축 최소 길이

 

<> 전체 코드 </>

class Solution {
    public long solution(int[][] dots) {
        int answer = 0;
        int x = 0; int y = 0;
        x = Math.abs(Math.max(dots[2][0],dots[1][0])-Math.min(dots[0][0], dots[3][0]));
        y = Math.abs(Math.max(dots[2][1],dots[1][1])-Math.min(dots[0][1], dots[3][1]));
        answer = x*y;
        return answer;
    }
}

반응형

댓글