Heartbeat, Heartbreak#370

Heartbeat, Heartbreak (Editorial)

One-Dimensional Case ()

:

When the problem is restricted to a single dimension (), the solution reduces to finding the length of the longest continuous segment of equal elements in the given sequence. This can be computed directly using a simple traversal of the array.

Multi-Dimensional Case (in this problem we have two dimensions)

:

In higher dimensions, the problem becomes more general. Consider an  — dimensional grid of size n_1 \times n_2 \times \dots \times n_N generated by the algorithm described in the statement. Let f(x_1, \dots, x_k) represent the length of the longest continuous segment of equal elements in the sequence (x_1, \dots, x_k) along a given dimension.

The key observation here is that the dimensions are independent. This means we can calculate the length of the longest continuous segment of equal elements separately for each dimension. Let L_1, L_2, \dots, L_N be the results for each respective dimension. Then, the answer for the  — dimensional grid is given by: \prod\limits_{i=1}^N L_i and can be computed in \mathcal{O}(\sum\limits_{i=1}^{N}n_i).

In our case we have only a two-dimensional grid of size n\times m, hence the complexity is \mathcal{O}(n+m).