内容中心

2026年比较好的浓缩果汁糖浆厂家选择推荐-广东加多利食品有限公司

To solve this problem, we need to count the number of axis-aligned rectangles completely filled with 1s in a given 2D grid. The approach leverages the concept of histogram-based counting, where we compute the number of rectangles for each row by considering the height of consecutive 1s ending at that row.

Approach

  1. Height Array: For each row, maintain a height array where each element at index j represents the number of consecutive 1s ending at the current row in column j. If the current cell is 0, the height is reset to 0.
  2. Count Rectangles in Histogram: For each row's height array, count the number of rectangles by considering all possible left and right columns. For each pair of columns (l, r), the number of rectangles with these columns as the left and right boundaries is the minimum height in the range [l, r] (since this minimum height determines how many rows above the current row can form valid rectangles).

Solution Code

def count_rectangles(grid):
    if not grid or not grid[0]:
        return 0
    m = len(grid)
    n = len(grid[0])
    height = [0] * n
    total = 0

    for row in grid:
        # Update the height array for the current row
        for j in range(n):
            height[j] = height[j] + 1 if row[j] == 1 else 0

        # Calculate the number of rectangles for the current height array
        row_sum = 0
        for l in range(n):
            current_min = height[l]
            row_sum += current_min
            for r in range(l + 1, n):
                current_min = min(current_min, height[r])
                row_sum += current_min

        total += row_sum

    return total

Explanation

  1. Height Array Update: For each row, we update the height array. If the current cell is 1, we increment the height from the previous row; otherwise, we reset it to 0. This helps us build a histogram for each row.
  2. Rectangle Counting: For each row's histogram, we consider all possible left columns l and expand to the right columns r. For each expansion, we keep track of the minimum height in the range [l, r]. The number of rectangles for this range is the minimum height, as it represents the number of valid rows (from the current row up to the minimum height rows) that form a rectangle with the columns l and r.

This approach efficiently counts all valid rectangles with a time complexity of O(m * n^2), which is feasible for the given constraints (m and n up to 200). The overall complexity is manageable, ensuring the solution runs quickly even for the upper limits of the input size.

Example

Let's take a 2x2 grid of all 1s:

  • Row 0: Height array is [1,1]. The number of rectangles here is 3 (1x1: 2, 1x2:1).
  • Row 1: Height array is [2,2]. The number of rectangles here is6 (1x1:2,1x2:1,2x1:2, 2x2:1).
  • Total:3+6=9, which is correct.

This example confirms the approach works for a simple case. The solution handles all edge cases, including grids with zeros and single rows/columns, correctly.

Answer: The function count_rectangles returns the number of valid rectangles in the grid. For the problem's input, the answer is the result of this function. However, since the input grid is not provided here, the code is the solution to compute the answer for any given grid. The final answer should be the output of this function when applied to the problem's input grid. For example, if the input grid is [[1,1],[1,1]], the answer is9.

The answer is the output of the function count_rectangles when applied to the input grid. For example, if the input grid is [[1,1],[1,1]], the answer is 9.

广东加多利食品有限公司

广东加多利食品有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?