-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay32.cpp
More file actions
76 lines (64 loc) · 1.9 KB
/
Day32.cpp
File metadata and controls
76 lines (64 loc) · 1.9 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
This problem was asked by Google.
You are given given a list of rectangles represented by min and max x- and y-coordinates. Compute whether or not a pair of rectangles overlap each other. If one rectangle completely covers another, it is considered overlapping.
For example, given the following rectangles:
{
"top_left": (1, 4),
"dimensions": (3, 3) # width, height
},
{
"top_left": (-1, 3),
"dimensions": (2, 1)
},
{
"top_left": (0, 5),
"dimensions": (4, 3)
}
return true as the first and third rectangle overlap each other.
*/
// rectangles.cpp
#include <iostream>
#include <vector>
using namespace std;
struct Rectangle
{
int x, y;
int w, h;
// Convert to [left, right) × (bottom, top] interval form
int left() const { return x; }
int right() const { return x + w; } // exclusive
int top() const { return y; }
int bottom() const { return y - h; } // inclusive
};
bool overlaps(const Rectangle &a, const Rectangle &b)
{
// One rectangle is completely to the left of the other
if (a.right() <= b.left() || b.right() <= a.left())
return false;
// One rectangle is completely above the other
if (a.bottom() >= b.top() || b.bottom() >= a.top())
return false;
return true; // otherwise intervals intersect on both axes
}
// Return true as soon as *any* pair overlaps
bool anyPairOverlaps(const vector<Rectangle> &rects)
{
const size_t n = rects.size();
for (size_t i = 0; i < n; ++i)
for (size_t j = i + 1; j < n; ++j)
if (overlaps(rects[i], rects[j]))
return true;
return false;
}
int main()
{
vector<Rectangle> rects = {
{1, 4, 3, 3}, // top_left (1,4), w=3, h=3
{-1, 3, 2, 1}, // top_left (-1,3), w=2, h=1
{0, 5, 4, 3} // top_left (0,5), w=4, h=3
};
cout << boolalpha
<< anyPairOverlaps(rects) // → true
<< '\n';
return 0;
}