diff --git a/3531. Count Covered Buildings b/3531. Count Covered Buildings new file mode 100644 index 0000000..0797ee0 --- /dev/null +++ b/3531. Count Covered Buildings @@ -0,0 +1,36 @@ +class Solution { +public: + int countCoveredBuildings(int n, vector>& buildings) { + unordered_map> rowToCol, colToRow; + for (auto &b : buildings) { + int x = b[0], y = b[1]; + rowToCol[x].insert(y); + colToRow[y].insert(x); + } + + int cnt = 0; + for (auto &b : buildings) { + int x = b[0], y = b[1]; + auto &cols = rowToCol[x]; + auto it = cols.lower_bound(y); + bool hasLeft = (it != cols.begin()); + bool hasRight = false; + if (it != cols.end()) { + auto it2 = it; ++it2; + hasRight = (it2 != cols.end()); + } + + auto &rows = colToRow[y]; + auto it3 = rows.lower_bound(x); + bool hasUp = (it3 != rows.begin()); + bool hasDown = false; + if (it3 != rows.end()) { + auto it4 = it3; ++it4; + hasDown = (it4 != rows.end()); + } + + if (hasLeft && hasRight && hasUp && hasDown) cnt++; + } + return cnt; + } +};