█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 2819

Problema 0xa6 - Submatriz de suma máxima en una matriz no muy grande

  • Autor: pradomaricon
  • Fecha: 2021-02-07 00:47:27 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.176 s 12 KBi
#2
Correcto
0.158 s 15 KBi
#3
Correcto
0.136 s 15 KBi
#4
Correcto
0.135 s 16 KBi
#5
Correcto
0.15 s 15 KBi
#6
Correcto
0.152 s 16 KBi
#7
Correcto
0.153 s 16 KBi
#8
Correcto
0.176 s 16 KBi
#9
Correcto
0.17 s 16 KBi
#10
Correcto
0.219 s 12 KBi
#11
Correcto
0.22 s 12 KBi
#12
Correcto
0.164 s 16 KBi
#13
Correcto
0.174 s 16 KBi
#14
Correcto
0.175 s 16 KBi
#15
Correcto
0.23 s 12 KBi
#16
Correcto
0.182 s 16 KBi
#17
Correcto
0.191 s 17 KBi
#18
Correcto
0.197 s 16 KBi
Puntos totales: 100 / 100

Código

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //input dimensiones
        String[] dimensiones = leer.nextLine().trim().split(" ");
        int filas = Integer.parseInt(dimensiones[0]);
        int columnas = Integer.parseInt(dimensiones[1]);
        int[][] ejemplo1 = new int[filas][columnas];


        //input de datos
        for (int i = 0; i < filas; i++) {
            String[] filaDada = leer.nextLine().trim().split(" ");
            int k = 0;
            for (int j = 0; j < columnas; j++) {

                ejemplo1[i][j] = Integer.parseInt(filaDada[k]);

                k++;
                //System.out.println(ejemplo1[i][j]);
            }
        }

        int toret = valorSubmatrizMaxima(ejemplo1, filas, columnas);

        System.out.println(toret);

    }

    public static Scanner leer = new Scanner(System.in);

    public static int valorSubmatrizMaxima(int[][] matriz, int filas, int columnas) {

        //crear matriz con suma de columnas acumulada
        int[][] matrizSumaColumnas = new int[filas][columnas];
        for (int i = 0; i < columnas; i++) {
            for (int j = 0; j < filas; j++) {
                if (j == 0) {
                    matrizSumaColumnas[j][i] = matriz[j][i];
                   
                } else {
                    matrizSumaColumnas[j][i] = matriz[j][i] + matrizSumaColumnas[j - 1][i];
                    
                }
            }
        }
        
        //algoritmo como el que teniamos en la busqueda del subarray maximo
        int maximoLocal = Integer.MIN_VALUE;
        int minimo, subMatriz;
        for (int i = 0; i < filas; i++) {
            for (int j = i; j < filas; j++) {
                minimo = 0;
                subMatriz = 0;
                for (int k = 0; k < columnas; k++) {
                    if (i == 0) {
                        subMatriz += matrizSumaColumnas[j][k];
                    } else {
                        subMatriz += matrizSumaColumnas[j][k] - matrizSumaColumnas[i - 1][k];
                    }
                    if (subMatriz < minimo) {
                        minimo = subMatriz;
                    }
                    if ((subMatriz - minimo) > maximoLocal) {
                        maximoLocal = subMatriz - minimo;
                    }
                }
            }
        }

        if (maximoLocal==0){
            maximoLocal=Integer.MIN_VALUE;
            for (int i = 0; i < columnas; i++) {
                for (int j = 0; j < filas; j++) {
                    if(matriz[j][i]>maximoLocal){
                        maximoLocal=matriz[j][i];
                    }
                }
        }
        }
        
        return maximoLocal;
        
    
        
    }
    
}