cswork / OA打开练习工作台

其他OA #6

Sub-matrix Sums

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)前缀和区间二分查找

Given an n x n matrix of integers and an integer value, threshold, determine the maximum size of a square sub-matrix such that for all square sub-matrices of that size, the sum of all elements in each square sub-matrix is less than or equal to the value threshold. Function Description Complete the function maxSubmatrixSumSize in the editor. maxSubmatrixSumSize has the following parameters:

    1. int[][] matrix: a 2D array of integers
    1. int threshold: the maximum sum of all square sub-matrices of a size must be less than or equal to this integer value Returns int: the maximum size of the square sub-matrix

Example 1

Input:

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
threshold = 4

Output:

0

Explanation: The maximum 1x1 matrix has a sum of 4. If threshold < 4 there is no size square sub-matrix that satisfies the condition. The answer is 0. 1x1 sub-matrices Maximum sub-matrix sum = 4

Example 2

Input:

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
threshold = 14

Output:

1

Explanation: The maximum 2x2 matrix has a sum of 14. If 4 ≤ threshold < 14, the maximum size of the square sub-matrix is 1. 2x2 sub-matrices Maximum sub-matrix sum = 14 (3 + 3 + 4 + 4 = 14)

Example 3

Input:

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
threshold = 27

Output:

2

Explanation: The maximum 3x3 matrix has a sum of 27. If 14 ≤ threshold < 27, the maximum size of the square sub-matrix is 2. 3x3 sub-matrices Maximum sub-matrix sum = 27 (2 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 = 27)