Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
perfect-rectangle.cpp 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-08-30 00:00 +08:00 . Update perfect-rectangle.cpp
// Time: O(n)
// Space: O(n)
class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
enum Location {L = 0, B = 1, R = 2, T = 3};
int left = numeric_limits<int>::max(), bottom = numeric_limits<int>::max(),
right = numeric_limits<int>::min(), top = numeric_limits<int>::min();
for (const auto& rect : rectangles) {
left = min(left, rect[L]);
bottom = min(bottom, rect[B]);
right = max(right, rect[R]);
top = max(top, rect[T]);
}
using P = pair<pair<int, int>, int>;
enum Corner {LB = 1, RB = 2, LT = 4, RT = 8};
unordered_map<int, unordered_map<int, int>> corner_count;
vector<P> corners{{{L, B}, LB}, {{R, B}, RB}, {{L, T}, LT}, {{R, T}, RT}};
for (const auto& rect : rectangles) {
for (const auto& corner : corners) {
const auto x = rect[corner.first.first];
const auto y = rect[corner.first.second];
if (corner_count[x][y] & corner.second) {
return false;
}
corner_count[x][y] |= corner.second;
}
}
bitset<16> is_valid;
is_valid[LB | RB] = is_valid[LB | LT] = is_valid[RB | RT] = is_valid[LT | RT] = is_valid[LB | RB | LT | RT] = true;
for (auto itx = corner_count.cbegin(); itx != corner_count.cend(); ++itx) {
const auto x = itx->first;
for (auto ity = itx->second.cbegin(); ity != itx->second.cend(); ++ity) {
const auto y = ity->first;
const auto mask = ity->second;
if ((left < x && x < right) || (bottom < y && y < top)) {
if (!is_valid[mask]) {
return false;
}
}
}
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助