From 88c467e9e4bcc5603bf63d14e0e5d024180de8d7 Mon Sep 17 00:00:00 2001 From: chayan das Date: Thu, 11 Dec 2025 23:35:52 +0530 Subject: [PATCH] Create 3531. Count Covered Buildings --- 3531. Count Covered Buildings | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3531. Count Covered Buildings 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; + } +};