PIXELBANKv9.1.0
Menu

Maximum Sum Rectangle in Image

Problem Statement

In image processing, finding regions of interest often involves locating rectangular areas with maximum pixel intensity sum.

Given a 2D matrix representing pixel intensities (can be negative for edge-detected images), find the maximum sum of any rectangular sub-region.

This is useful for:

  • Detecting bright regions in astronomical images
  • Finding high-contrast areas after edge detection
  • Locating regions of interest in thermal imaging

Constraints

  • 1≤rows,cols≤1001 \leq rows, cols \leq 100
  • āˆ’1000≤matrix[i][j]≤1000-1000 \leq matrix[i][j] \leq 1000

Approach

Use Kadane's algorithm extended to 2D by fixing left and right columns, then computing max sum subarray for each row range.

Example:

Input:
matrix = [[1, -2, 3], [-4, 5, -6], [7, -8, 9]]
Output:
9
Reasoning:

The maximum sum rectangle is just the single cell with value 9.

solution.py

Test Results

0/0
Run code to see test results.