Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 11. 17. 23:45

https://leetcode.com/problems/rectangle-area/

 

Rectangle Area - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

수학적인 머리가 필요한 문제.
몇 번 시도하다가 걍 솔루션 봄 =_=;

class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        
        ar_a = (ay2 - ay1) * (ax2 - ax1)
        ar_b = (by2 - by1) * (bx2 - bx1)
        
        l = max(ax1, bx1)
        r = min(ax2, bx2)
        x_o = r - l
        
        t = min(ay2, by2)
        b = max(ay1, by1)
        y_o = t - b
        
        ar_o = 0
        
        if x_o > 0 and y_o > 0:
            ar_o = x_o * y_o
            
        return ar_a + ar_b - ar_o
반응형