Skip to content

Commit ec76b7d

Browse files
committed
391
1 parent 76a45bb commit ec76b7d

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

leetcode/391/a.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[0,0,3,3],[1,1,2,2]]

leetcode/391/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInput.hpp"
7+
#include "solution.cpp"
8+
using namespace std;
9+
10+
int main()
11+
{
12+
LeetCodeInput li("a.in");
13+
14+
vector<vector<int>> mat_1 = li.get_vvi(0);
15+
// vector<int> arr_1 = li.get_vi(0);
16+
// int num_1 = li.get_i(0);
17+
// string s_1 = li.get_s(0);
18+
19+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
20+
Solution solution;
21+
auto output = solution.isRectangleCover(mat_1);
22+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
23+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
24+
25+
cout << output << endl;
26+
27+
// for (int i: output){
28+
// cout << i << ' ';
29+
// }
30+
31+
// for (auto i: output){
32+
// for (int j: i){
33+
// cout << j << ' ';
34+
// }
35+
// cout << '\n';
36+
// }
37+
38+
return 0;
39+
}

leetcode/391/solution.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
3+
time: O(nlog(n))
4+
space: O(n)
5+
6+
Runtime: 129 ms, faster than 94.04% of C++ online submissions for Perfect Rectangle.
7+
Memory Usage: 37.9 MB, less than 20.00% of C++ online submissions for Perfect Rectangle.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
bool isRectangleCover(vector<vector<int>>& rectangles) {
21+
/// stream[y] = { open_map, close_map }
22+
/// x_map[x1] = x2
23+
map<int, array<map<int, int>, 2>> stream;
24+
for (auto & rect : rectangles){
25+
if (stream[rect[1]][0].find(rect[0]) != stream[rect[1]][0].end() && stream[rect[1]][0][rect[0]] == rect[2]) return false;
26+
stream[rect[1]][0].insert({rect[0], rect[2]});
27+
stream[rect[3]][1].insert({rect[0], rect[2]});
28+
}
29+
30+
/// merge bottom
31+
if (!merge_ranges((*begin(stream)).second[0])) return false;
32+
/// bottom check
33+
if ((*stream.begin()).second[0].size() != 1) return false;
34+
/// merge top
35+
if (!merge_ranges((*rbegin(stream)).second[1])) return false;
36+
/// top check
37+
if ((*rbegin(stream)).second[1] != (*begin(stream)).second[0]) return false;
38+
/// middle check
39+
auto it_end = prev((stream.end()));
40+
for (auto it = next(stream.begin()); it != it_end; ++it){
41+
if (!merge_ranges((*it).second[0])) return false;
42+
if (!merge_ranges((*it).second[1])) return false;
43+
if ((*it).second[0] != (*it).second[1]) return false;
44+
}
45+
46+
return true;
47+
}
48+
49+
inline bool merge_ranges(std::map<int, int> & ranges){
50+
/// empty range does not equal nonempty ones, but would cause infinite loop
51+
if (ranges.empty()) return false;
52+
auto former = begin(ranges);
53+
auto cur = next(begin(ranges));
54+
map<int, int>::iterator to_erase;
55+
while (cur != end(ranges)){
56+
if ((*cur).first == (*former).second){
57+
(*former).second = (*cur).second;
58+
to_erase = cur++;
59+
ranges.erase(to_erase);
60+
} else if ((*cur).first < (*former).second){
61+
return false;
62+
} else {
63+
former = cur;
64+
++cur;
65+
}
66+
}
67+
return true;
68+
}
69+
};
70+
71+
const static auto initialize = [] {
72+
std::ios::sync_with_stdio(false);
73+
std::cin.tie(nullptr);
74+
std::cout.tie(nullptr);
75+
return nullptr;
76+
}();

0 commit comments

Comments
 (0)