描述
(搬运)
Consider a matrix M
with dimensions width
* height
, such that every cell has value 0
or 1
, and any square sub-matrix of M
of size sideLength
* sideLength
has at most maxOnes
ones.
Return the maximum possible number of ones that the matrix M
can have.
Example 1:
1 | Input: width = 3, height = 3, sideLength = 2, maxOnes = 1 |
Example 2:
1 | Input: width = 3, height = 3, sideLength = 2, maxOnes = 2 |
Constraints:
1 | 1 <= width, height <= 100 |
思路
这个贪心姿势真是想不到… 或者说就算想到, 自己也会立刻否定它的正确性…contest时总是觉得中心对称好像也行:(
暂时当做结论题好了, 要达到全局最优, 每个滑窗必然有重复pattern, 然后我们把sideLength * sideLength
的滑窗flatten一下, 分别利用大矩形在滑窗内的映射点, 计算每一个位置的贡献, 然后统计前maxOnes
大的贡献之和.
代码
1 | class Solution(object): |