Official

B - ビルの高さ / Building Heights Editorial by admin Editorial for printing

swe-2-high

Editorial

The height of building \(j\) is defined as the number of # cells in column \(j\), so we only need to count # characters column by column.

The program first reads all \(H\) strings into a vector<string> so the whole figure is stored. Then for each column \(j\) from \(0\) to \(W-1\), it scans every row \(i\) and increments a counter whenever g[i][j] is #. The count for each column is printed separated by spaces, with a newline after the last one.

Why it is correct: the inner loop literally computes the definition of the height — the number of # cells in that column — and doing this for every column yields the heights in left-to-right order.

Complexity: each cell is inspected exactly once, so the running time is \(O(HW)\), and storing the whole grid costs \(O(HW)\) space. The output consists of \(W\) numbers.

posted:
last update: