果冻,21 20 18 17 字节
ṡṂ€€×"
‘×¥\ç"Ụ€FṀ
在线尝试!或验证所有测试用例。
背景
令M为位矩阵,例如
0 0 0 1 1 0 0 0 0
1 1 0 1 1 0 0 1 0
1 1 0 1 1 1 1 1 0
1 1 0 0 1 1 1 0 0
0 1 0 0 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 0 1 1 1 0
我们从对M的每一列中1位的数目进行计数开始,每次遇到0位时都重新设置计数。
对于我们的示例矩阵,这给出了
0 0 0 1 1 0 0 0 0
1 1 0 2 2 0 0 1 0
2 2 0 3 3 1 1 2 0
3 3 0 0 4 2 2 0 0
0 4 0 0 5 3 3 1 1
1 5 1 1 6 4 4 2 2
2 6 2 2 0 5 5 3 0
接下来,我们计算每一行的所有连续子列表。我们通过生成所有长度为k的切片来实现此目的,其中k在1和每行中的条目数之间变化。
对于倒数第二行,
[1], [5], [1], [1], [6], [4], [4], [2], [2]
[1, 5], [5, 1], [1, 1], [1, 6], [6, 4], [4, 4], [4, 2], [2, 2]
[1, 5, 1], [5, 1, 1], [1, 1, 6], [1, 6, 4], [6, 4, 4], [4, 4, 2], [4, 2, 2]
[1, 5, 1, 1], [5, 1, 1, 6], [1, 1, 6, 4], [1, 6, 4, 4], [6, 4, 4, 2], [4, 4, 2, 2]
[1, 5, 1, 1, 6], [5, 1, 1, 6, 4], [1, 1, 6, 4, 4], [1, 6, 4, 4, 2], [6, 4, 4, 2, 2]
[1, 5, 1, 1, 6, 4], [5, 1, 1, 6, 4, 4], [1, 1, 6, 4, 4, 2], [1, 6, 4, 4, 2, 2]
[1, 5, 1, 1, 6, 4, 4], [5, 1, 1, 6, 4, 4, 2], [1, 1, 6, 4, 4, 2, 2]
[1, 5, 1, 1, 6, 4, 4, 2], [5, 1, 1, 6, 4, 4, 2, 2]
[1, 5, 1, 1, 6, 4, 4, 2, 2]
接下来,我们将每个切片映射到其最小值与长度的乘积。对于每个切片,这将计算以给定切片为底行的最大高度的1位矩形的面积。
对于示例矩阵倒数第二行的长度为3的切片,得出
3 3 3 3 12 6 6
剩下要做的就是在所有行的所有切片中使用最大值。
对于我们的示例矩阵,得出12。
怎么运行的
‘×¥\ç"Ụ€FṀ Main link. Argument: M (2D list of bits)
\ Reduce the columns of M by the link to the left.
¥ Combine the two atoms to the left into a dyadic chain.
‘ Increment the left argument.
× Multiply the result with the right argument.
Ụ€ Grade up each; yield the indices of each row of M, sorted by their
values. The order is not important here; we just need the indices.
ç" Apply the helper link to each element of the result to the left and
the corresponding element of the result to the right.
F Flatten the resulting, nested list.
Ṁ Extract the maximum.
ṡṂ€€×" Helper link. Arguments: R (row), X (indices of R)
ṡ For each k in X, split R into overlapping slices of length k.
Ṁ€€ Compute the minimum of each individual slice.
×" Multiply the minima of all slices of length k by k.
plow
。