Skip to content
Discussion options

You must be logged in to vote

We need to determine for each building whether it has at least one building in all four directions: left, right, above, and below. A direct check for each building would be inefficient (O(m²) in worst case). Instead, we can group buildings by their x‑coordinate and y‑coordinate, then use sorting to efficiently determine coverage.

Approach:

  1. Group by x and y

    • Create a hash map xMap that maps each x‑coordinate to a list of y‑coordinates of buildings in that column.
    • Create a hash map yMap that maps each y‑coordinate to a list of x‑coordinates of buildings in that row.
  2. Determine vertical coverage (above/below)
    For each x in xMap:

    • Sort the list of y‑coordinates.
    • For each building (x, y) i…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Dec 11, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 11, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants