forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
28 lines (28 loc) · 1.08 KB
/
Copy paths1.cpp
File metadata and controls
28 lines (28 loc) · 1.08 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
class Solution {
private:
inline string getKey(int x, int y) { return to_string(x) + " " + to_string(y); }
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
if (rectangles.empty() || rectangles[0].empty()) return false;
int x1 = INT_MAX, y1 = INT_MAX, x2 = INT_MIN, y2 = INT_MIN, area = 0;
unordered_set<string> s;
for (auto v : rectangles) {
x1 = min(x1, v[0]);
y1 = min(y1, v[1]);
x2 = max(x2, v[2]);
y2 = max(y2, v[3]);
area += (v[2] - v[0]) * (v[3] - v[1]);
string a = getKey(v[0], v[1]), b = getKey(v[0], v[3]), c = getKey(v[2], v[1]), d = getKey(v[2], v[3]);
if (!s.erase(a)) s.insert(a);
if (!s.erase(b)) s.insert(b);
if (!s.erase(c)) s.insert(c);
if (!s.erase(d)) s.insert(d);
}
return s.count(getKey(x1, y1))
&& s.count(getKey(x1, y2))
&& s.count(getKey(x2, y1))
&& s.count(getKey(x2, y2))
&& s.size() == 4
&& area == (x2 - x1) * (y2 - y1);
}
};