-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (48 loc) · 1.79 KB
/
main.cpp
File metadata and controls
54 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Source: https://leetcode.com/problems/rectangle-area
// Title: Rectangle Area
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:**
// https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png
//
// ```
// 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:**
//
// - `-10^4 <= ax1 <= ax2 <= 10^4`
// - `-10^4 <= ay1 <= ay2 <= 10^4`
// - `-10^4 <= bx1 <= bx2 <= 10^4`
// - `-10^4 <= by1 <= by2 <= 10^4`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
using namespace std;
// Find the overlap range of x and y.
// The answer is the sum of the two area minus the overlapping area.
class Solution {
public:
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
auto cx1 = max(ax1, bx1), cy1 = max(ay1, by1), cx2 = min(ax2, bx2), cy2 = min(ay2, by2);
auto aArea = (ax2 - ax1) * (ay2 - ay1);
auto bArea = (bx2 - bx1) * (by2 - by1);
auto cArea = max(cx2 - cx1, 0) * max(cy2 - cy1, 0);
return aArea + bArea - cArea;
}
};