Programação CompetitivaDifícil

Retângulo de Soma Máxima

Guia detalhado e implementação de Python para o problema 'Retângulo de Soma Máxima'.

Declaração do problema

Difícil

Escreva uma função max_sum_rectangle(matrix) que encontre e retorne a soma máxima de uma submatriz em uma matriz 2D de inteiros.

Restrições
  • 1 <= len(matrix), len(matrix[0]) <= 100
  • -1000 <= matrix[i][j] <= 1000

Exemplos

Example 1
Input
max_sum_rectangle([[1, 2, -1, -4, -20], [-8, -3, 4, 2, 1], [3, 8, 10, 1, 3], [-4, -1, 1, 7, -6]])
Output
29
Explanation

The sub-matrix from row 1, column 1 to row 3, column 3 has the maximum sum of 29.

Example 2
Input
max_sum_rectangle([[-1, -2], [-3, -4]])
Output
-1
Explanation

The maximum sum is -1, which is the cell at (0, 0).

Need a Hint?
Considere o uso de estruturas de dados específicas da Programação Dinâmica, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.