엔지니어 게시판
LeetCode 솔루션 분류

[11/17] 223. Rectangle Area

컨텐츠 정보

본문

Medium
15641484Add to ListShare

Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.

The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).

The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).

 

Example 1:

Rectangle Area
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45

Example 2:

Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16

 

Constraints:

  • -104 <= ax1 <= ax2 <= 104
  • -104 <= ay1 <= ay2 <= 104
  • -104 <= bx1 <= bx2 <= 104
  • -104 <= by1 <= by2 <= 104
Accepted
183,913
Submissions
414,573
태그

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 113 ms, faster than 43.28% of Python3 online submissions for Rectangle Area.
Memory Usage: 13.8 MB, less than 99.88% of Python3 online submissions for Rectangle Area.
class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        top_right_y = min(ay2, by2)
        top_right_x = min(ax2, bx2)
        
        bottom_left_y = max(ay1, by1)
        bottom_left_x = max(ax1, bx1)
        
        x_overlap = max(top_right_x - bottom_left_x,0)
        y_overlap = max(top_right_y - bottom_left_y, 0)        
        return ((ax2-ax1)*(ay2-ay1) + (bx2-bx1)*(by2-by1)) - x_overlap*y_overlap
전체 194 / 3 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 751 명
  • 오늘 방문자 7,004 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,537 명
알림 0