Prerrequisito: algoritmo de Kadane
Dada una array 2D arr[][] de dimensión N*M , la tarea es encontrar la subarray de suma máxima de la array arr[][] .
Ejemplos:
Entrada: array[][] = {{0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2}}
Salida: 15
Explicación: La subarray {{9, 2}, {-4, 1}, {-1, 8}} tiene una suma 15, que es la suma máxima posible.Entrada: arr[][] = {{1, 2}, {-5, -7}}
Salida: 3
Enfoque ingenuo: el enfoque más simple es generar todas las subarrays posibles a partir de la array dada y calcular su suma. Finalmente, imprima la suma máxima obtenida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find maximum sum submatrix void maxSubmatrixSum( vector<vector<int> > matrix) { // Stores the number of rows // and columns in the matrix int r = matrix.size(); int c = matrix[0].size(); // Stores maximum submatrix sum int maxSubmatrix = 0; // Take each row as starting row for (int i = 0; i < r; i++) { // Take each column as the // starting column for (int j = 0; j < c; j++) { // Take each row as the // ending row for (int k = i; k < r; k++) { // Take each column as // the ending column for (int l = j; l < c; l++) { // Stores the sum of submatrix // having topleft index(i, j) // and bottom right index (k, l) int sumSubmatrix = 0; // Iterate the submatrix // row-wise and calculate its sum for (int m = i; m <= k; m++) { for (int n = j; n <= l; n++) { sumSubmatrix += matrix[m][n]; } } // Update the maximum sum maxSubmatrix = max(maxSubmatrix, sumSubmatrix); } } } } // Print the answer cout << maxSubmatrix; } // Driver Code int main() { vector<vector<int> > matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; maxSubmatrixSum(matrix); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find maximum sum submatrix static void maxSubmatrixSum(int[][] matrix) { // Stores the number of rows // and columns in the matrix int r = matrix.length; int c = matrix[0].length; // Stores maximum submatrix sum int maxSubmatrix = 0; // Take each row as starting row for (int i = 0; i < r; i++) { // Take each column as the // starting column for (int j = 0; j < c; j++) { // Take each row as the // ending row for (int k = i; k < r; k++) { // Take each column as // the ending column for (int l = j; l < c; l++) { // Stores the sum of submatrix // having topleft index(i, j) // and bottom right index (k, l) int sumSubmatrix = 0; // Iterate the submatrix // row-wise and calculate its sum for (int m = i; m <= k; m++) { for (int n = j; n <= l; n++) { sumSubmatrix += matrix[m][n]; } } // Update the maximum sum maxSubmatrix = Math.max(maxSubmatrix, sumSubmatrix); } } } } // Print the answer System.out.println(maxSubmatrix); } // Driver Code public static void main(String[] args) { int[][] matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; maxSubmatrixSum(matrix); } } // This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach # Function to find maximum sum submatrix def maxSubmatrixSum(matrix): # Stores the number of rows # and columns in the matrix r = len(matrix) c = len(matrix[0]) # Stores maximum submatrix sum maxSubmatrix = 0 # Take each row as starting row for i in range(r): # Take each column as the # starting column for j in range(c): # Take each row as the # ending row for k in range(i, r): # Take each column as # the ending column for l in range(j, c): # Stores the sum of submatrix # having topleft index(i, j) # and bottom right index (k, l) sumSubmatrix = 0 # Iterate the submatrix # row-wise and calculate its sum for m in range(i, k + 1): for n in range(j, l + 1): sumSubmatrix += matrix[m][n] # Update the maximum sum maxSubmatrix= max(maxSubmatrix, sumSubmatrix) # Print the answer print (maxSubmatrix) # Driver Code if __name__ == '__main__': matrix = [ [ 0, -2, -7, 0 ], [ 9, 2, -6, 2 ], [ -4, 1, -4, 1 ], [ -1, 8, 0, -2 ] ] maxSubmatrixSum(matrix) # This code is contributed by mohit kumar 29.
C#
// C# program to implement // the above approach using System; public class GFG { // Function to find maximum sum submatrix static void maxSubmatrixSum(int[,] matrix) { // Stores the number of rows // and columns in the matrix int r = matrix.GetLength(0); int c = matrix.GetLength(1); // Stores maximum submatrix sum int maxSubmatrix = 0; // Take each row as starting row for (int i = 0; i < r; i++) { // Take each column as the // starting column for (int j = 0; j < c; j++) { // Take each row as the // ending row for (int k = i; k < r; k++) { // Take each column as // the ending column for (int l = j; l < c; l++) { // Stores the sum of submatrix // having topleft index(i, j) // and bottom right index (k, l) int sumSubmatrix = 0; // Iterate the submatrix // row-wise and calculate its sum for (int m = i; m <= k; m++) { for (int n = j; n <= l; n++) { sumSubmatrix += matrix[m, n]; } } // Update the maximum sum maxSubmatrix = Math.Max(maxSubmatrix, sumSubmatrix); } } } } // Print the answer Console.WriteLine(maxSubmatrix); } // Driver Code public static void Main(String []args) { int[,] matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; maxSubmatrixSum(matrix); } } // This code is contributed by sanjoy_62.
Javascript
<script> // Javascript program for the above approach // Function to find maximum sum submatrix function maxSubmatrixSum(matrix) { // Stores the number of rows // and columns in the matrix var r = matrix.length; var c = matrix[0].length; // Stores maximum submatrix sum var maxSubmatrix = 0; // Take each row as starting row for (i = 0; i < r; i++) { // Take each column as the // starting column for (j = 0; j < c; j++) { // Take each row as the // ending row for (k = i; k < r; k++) { // Take each column as // the ending column for (l = j; l < c; l++) { // Stores the sum of submatrix // having topleft index(i, j) // and bottom right index (k, l) var sumSubmatrix = 0; // Iterate the submatrix // row-wise and calculate its sum for (m = i; m <= k; m++) { for (n = j; n <= l; n++) { sumSubmatrix += matrix[m][n]; } } // Update the maximum sum maxSubmatrix = Math.max(maxSubmatrix, sumSubmatrix); } } } } // Print the answer document.write(maxSubmatrix); } // Driver Code var matrix = [ [ 0, -2, -7, 0 ], [ 9, 2, -6, 2 ], [ -4, 1, -4, 1 ], [ -1, 8, 0, -2 ] ]; maxSubmatrixSum(matrix); // This code contributed by umadevi9616 </script>
15
Complejidad de Tiempo: O(N 6 )
Espacio Auxiliar: O(1)
Enfoque eficiente usando el algoritmo de Kadane: El enfoque anterior se puede optimizar usando las siguientes observaciones:
- Corrija la columna inicial y final de la subarray requerida, digamos inicio y final respectivamente.
- Ahora, itere cada fila y agregue la suma de la fila desde la columna inicial hasta la final a sumSubmatrix e insértela en una array. Después de iterar cada fila, realice el Algoritmo de Kadane en esta array recién creada. Si la suma obtenida al aplicar el algoritmo de Kadane es mayor que la suma máxima general, actualice la suma máxima general.
- En el paso anterior, la suma de las filas desde la columna inicial hasta la final se puede calcular en tiempo constante mediante la creación de una array auxiliar de tamaño N*M que contiene la suma del prefijo de cada fila.
Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos maxSum como INT_MIN , para almacenar la suma máxima del subarreglo.
- Cree una array prefMatrix[N][M] que almacene la suma de la array de prefijos de cada fila de la array dada.
- Recorra la array por filas utilizando i como índice de fila y j como índice de columna y realice los siguientes pasos:
- Si el valor de i es 0 , establezca prefMatrix[i][j] = A[i][j] .
- De lo contrario, establezca prefMatrix[i][j] = prefMatrix[i][j – 1] + A[i][j] .
- Ahora, para todas las combinaciones posibles de índice inicial y final de las columnas de la subarray sobre el rango [0, M], realice los siguientes pasos:
- Inicialice una array auxiliar A[] para almacenar la suma máxima de cada fila de la subarray actual.
- Encuentre la suma desde la columna inicial hasta la final usando prefMatrix de la siguiente manera:
- Si el valor de inicio es positivo, almacene la suma requerida S como prefMatrix[i][end] – prefMatrix[i][start – 1] .
- De lo contrario, actualice S como prefMatrix[i][end] .
- Inserte S en una array arr[] .
- Después de iterar todas las filas en la subarray, realice el algoritmo de Kadane en la array A[] y actualice la suma máxima maxSum como el máximo de maxSum y el valor obtenido al realizar el algoritmo de Kadane en este paso.
- Después de completar los pasos anteriores, imprima el valor de maxSum como resultado.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find maximum continuous // maximum sum in the array int kadane(vector<int> v) { // Stores current and maximum sum int currSum = 0; int maxSum = INT_MIN; // Traverse the array v for (int i = 0; i < (int)v.size(); i++) { // Add the value of the // current element currSum += v[i]; // Update the maximum sum if (currSum > maxSum) { maxSum = currSum; } if (currSum < 0) { currSum = 0; } } // Return the maximum sum return maxSum; } // Function to find the maximum // submatrix sum void maxSubmatrixSum( vector<vector<int> > A) { // Store the rows and columns // of the matrix int r = A.size(); int c = A[0].size(); // Create an auxiliary matrix int** prefix = new int*[r]; // Traverse the matrix, prefix // and initialize it will all 0s for (int i = 0; i < r; i++) { prefix[i] = new int; for (int j = 0; j < c; j++) { prefix[i][j] = 0; } } // Calculate prefix sum of all // rows of matrix A[][] and // store in matrix prefix[] for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { // Update the prefix[][] if (j == 0) prefix[i][j] = A[i][j]; else prefix[i][j] = A[i][j] + prefix[i][j - 1]; } } // Store the maximum submatrix sum int maxSum = INT_MIN; // Iterate for starting column for (int i = 0; i < c; i++) { // Iterate for last column for (int j = i; j < c; j++) { // To store current array // elements vector<int> v; // Traverse every row for (int k = 0; k < r; k++) { // Store the sum of the // kth row int el = 0; // Update the prefix // sum if (i == 0) el = prefix[k][j]; else el = prefix[k][j] - prefix[k][i - 1]; // Push it in a vector v.push_back(el); } // Update the maximum // overall sum maxSum = max(maxSum, kadane(v)); } } // Print the answer cout << maxSum << "\n"; } // Driver Code int main() { vector<vector<int> > matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; // Function Call maxSubmatrixSum(matrix); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find maximum continuous // maximum sum in the array static int kadane(Vector<Integer> v) { // Stores current and maximum sum int currSum = 0; int maxSum = Integer.MIN_VALUE; // Traverse the array v for (int i = 0; i < (int)v.size(); i++) { // Add the value of the // current element currSum += v.get(i); // Update the maximum sum if (currSum > maxSum) { maxSum = currSum; } if (currSum < 0) { currSum = 0; } } // Return the maximum sum return maxSum; } // Function to find the maximum // submatrix sum static void maxSubmatrixSum(int [][]A) { // Store the rows and columns // of the matrix int r = A.length; int c = A[0].length; // Create an auxiliary matrix int [][]prefix = new int[r][]; // Traverse the matrix, prefix // and initialize it will all 0s for (int i = 0; i < r; i++) { prefix[i] = new int; for (int j = 0; j < c; j++) { prefix[i][j] = 0; } } // Calculate prefix sum of all // rows of matrix A[][] and // store in matrix prefix[] for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { // Update the prefix[][] if (j == 0) prefix[i][j] = A[i][j]; else prefix[i][j] = A[i][j] + prefix[i][j - 1]; } } // Store the maximum submatrix sum int maxSum = Integer.MIN_VALUE; // Iterate for starting column for (int i = 0; i < c; i++) { // Iterate for last column for (int j = i; j < c; j++) { // To store current array // elements Vector<Integer> v = new Vector<Integer>(); // Traverse every row for (int k = 0; k < r; k++) { // Store the sum of the // kth row int el = 0; // Update the prefix // sum if (i == 0) el = prefix[k][j]; else el = prefix[k][j] - prefix[k][i - 1]; // Push it in a vector v.add(el); } // Update the maximum // overall sum maxSum = Math.max(maxSum, kadane(v)); } } // Print the answer System.out.print(maxSum+ "\n"); } // Driver Code public static void main(String[] args) { int [][]matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; // Function Call maxSubmatrixSum(matrix); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach import sys # Function to find maximum continuous # maximum sum in the array def kadane(v): # Stores current and maximum sum currSum = 0 maxSum = -sys.maxsize - 1 # Traverse the array v for i in range(len(v)): # Add the value of the # current element currSum += v[i] # Update the maximum sum if (currSum > maxSum): maxSum = currSum if (currSum < 0): currSum = 0 # Return the maximum sum return maxSum # Function to find the maximum # submatrix sum def maxSubmatrixSum(A): # Store the rows and columns # of the matrix r = len(A) c = len(A[0]) # Create an auxiliary matrix # Traverse the matrix, prefix # and initialize it will all 0s prefix = [[0 for i in range(c)] for j in range(r)] # Calculate prefix sum of all # rows of matrix A[][] and # store in matrix prefix[] for i in range(r): for j in range(c): # Update the prefix[][] if (j == 0): prefix[i][j] = A[i][j] else: prefix[i][j] = A[i][j] + prefix[i][j - 1] # Store the maximum submatrix sum maxSum = -sys.maxsize - 1 # Iterate for starting column for i in range(c): # Iterate for last column for j in range(i, c): # To store current array # elements v = [] # Traverse every row for k in range(r): # Store the sum of the # kth row el = 0 # Update the prefix # sum if (i == 0): el = prefix[k][j] else: el = prefix[k][j] - prefix[k][i - 1] # Push it in a vector v.append(el) # Update the maximum # overall sum maxSum = max(maxSum, kadane(v)) # Print the answer print(maxSum) # Driver Code matrix = [ [ 0, -2, -7, 0 ], [ 9, 2, -6, 2 ], [ -4, 1, -4, 1 ], [ -1, 8, 0, -2 ] ] # Function Call maxSubmatrixSum(matrix) # This code is contributed by rag2127
C#
// C# program for the above approach using System; using System.Collections.Generic; public class GFG{ // Function to find maximum continuous // maximum sum in the array static int kadane(List<int> v) { // Stores current and maximum sum int currSum = 0; int maxSum = int.MinValue; // Traverse the array v for (int i = 0; i < (int)v.Count; i++) { // Add the value of the // current element currSum += v[i]; // Update the maximum sum if (currSum > maxSum) { maxSum = currSum; } if (currSum < 0) { currSum = 0; } } // Return the maximum sum return maxSum; } // Function to find the maximum // submatrix sum static void maxSubmatrixSum(int [,]A) { // Store the rows and columns // of the matrix int r = A.GetLength(0); int c = A.GetLength(1); // Create an auxiliary matrix int [,]prefix = new int[r,c]; // Traverse the matrix, prefix // and initialize it will all 0s for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { prefix[i,j] = 0; } } // Calculate prefix sum of all // rows of matrix [,]A and // store in matrix prefix[] for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { // Update the prefix[,] if (j == 0) prefix[i,j] = A[i,j]; else prefix[i,j] = A[i,j] + prefix[i,j - 1]; } } // Store the maximum submatrix sum int maxSum = int.MinValue; // Iterate for starting column for (int i = 0; i < c; i++) { // Iterate for last column for (int j = i; j < c; j++) { // To store current array // elements List<int> v = new List<int>(); // Traverse every row for (int k = 0; k < r; k++) { // Store the sum of the // kth row int el = 0; // Update the prefix // sum if (i == 0) el = prefix[k,j]; else el = prefix[k,j] - prefix[k,i - 1]; // Push it in a vector v.Add(el); } // Update the maximum // overall sum maxSum = Math.Max(maxSum, kadane(v)); } } // Print the answer Console.Write(maxSum+ "\n"); } // Driver Code public static void Main(String[] args) { int [,]matrix = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; // Function Call maxSubmatrixSum(matrix); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript program for the above approach // Function to find maximum continuous // maximum sum in the array function kadane(v) { // Stores current and maximum sum let currSum = 0; let maxSum = Number.MIN_VALUE; // Traverse the array v for (let i = 0; i < v.length; i++) { // Add the value of the // current element currSum += v[i]; // Update the maximum sum if (currSum > maxSum) { maxSum = currSum; } if (currSum < 0) { currSum = 0; } } // Return the maximum sum return maxSum; } // Function to find the maximum // submatrix sum function maxSubmatrixSum(A) { // Store the rows and columns // of the matrix let r = A.length; let c = A[0].length; // Create an auxiliary matrix let prefix = new Array(r); // Traverse the matrix, prefix // and initialize it will all 0s for (let i = 0; i < r; i++) { prefix[i] = new Array(c); for (let j = 0; j < c; j++) { prefix[i][j] = 0; } } // Calculate prefix sum of all // rows of matrix A[][] and // store in matrix prefix[] for (let i = 0; i < r; i++) { for (let j = 0; j < c; j++) { // Update the prefix[][] if (j == 0) prefix[i][j] = A[i][j]; else prefix[i][j] = A[i][j] + prefix[i][j - 1]; } } // Store the maximum submatrix sum let maxSum = Number.MIN_VALUE; // Iterate for starting column for (let i = 0; i < c; i++) { // Iterate for last column for (let j = i; j < c; j++) { // To store current array // elements let v = []; // Traverse every row for (let k = 0; k < r; k++) { // Store the sum of the // kth row let el = 0; // Update the prefix // sum if (i == 0) el = prefix[k][j]; else el = prefix[k][j] - prefix[k][i - 1]; // Push it in a vector v.push(el); } // Update the maximum // overall sum maxSum = Math.max(maxSum, kadane(v)); } } // Print the answer document.write(maxSum+ "<br>"); } // Driver Code let matrix=[[ 0, -2, -7, 0 ], [ 9, 2, -6, 2 ], [ -4, 1, -4, 1 ], [ -1, 8, 0, -2 ]]; // Function Call maxSubmatrixSum(matrix); // This code is contributed by unknown2108 </script>
15
Complejidad de Tiempo: O(N 3 )
Espacio Auxiliar: O(N 2 )
Tema relacionado: Subarrays, subsecuencias y subconjuntos en array
Publicación traducida automáticamente
Artículo escrito por priyankrastogi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA