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

Envío 6293

Problema 0x50 - Rectángulo de máxima área dentro de un histograma enorme

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 23:27:35 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.114 s 18 KBi
#2
Correcto
0.126 s 17 KBi
#3
Correcto
0.087 s 16 KBi
#4
Correcto
0.089 s 16 KBi
#5
Correcto
0.087 s 16 KBi
#6
Correcto
0.089 s 16 KBi
#7
Correcto
0.095 s 16 KBi
#8
Correcto
0.118 s 18 KBi
#9
Correcto
0.076 s 16 KBi
#10
Correcto
0.107 s 17 KBi
#11
Correcto
0.083 s 16 KBi
#12
Correcto
0.081 s 16 KBi
#13
Correcto
0.082 s 17 KBi
#14
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.601 s 125 KBi
#15
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.533 s 125 KBi
#16
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.573 s 125 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.556 s 125 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.539 s 125 KBi
#19
Tiempo límite excedido
1.049 s 125 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.557 s 125 KBi
#21
Tiempo límite excedido
1.135 s 103 KBi
#22
Tiempo límite excedido
1.06 s 125 KBi
#23
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.57 s 125 KBi
#24
Tiempo límite excedido
1.071 s 103 KBi
#25
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.596 s 125 KBi
#26
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.567 s 125 KBi
#27
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.584 s 125 KBi
#28
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.5 s 125 KBi
#29
Tiempo límite excedido
1.059 s 99 KBi
Puntos totales: 45 / 100

Código

import java.util.HashMap;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    private static final HashMap<Integer, Integer> memoization = new HashMap<Integer, Integer>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }

        int max = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int h = findMinHeight(i, j, array);
                int b = j - i + 1;
                int area = h * b;

                if (area > max) {
                    max = area;
                }
            }
        }

        System.out.println(max);
    }

    private static int findMinHeight(int start, int end, int[] array) {
        int key = new Key(start, end).hashCode();
        if (memoization.containsKey(key)) {
            memoization.get(key);
        }

        if (start == end) {
            memoization.put(key, array[start]);
            return array[start];
        }

        int value = array[start];
        int recResponse = findMinHeight(start+1, end, array);

        int min = Math.min(value, recResponse);
        memoization.put(key, min);
        return min;
    }

    private static class Key {
        private final int start;
        private final int end;

        public Key(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public int hashCode() {
            return Objects.hash(start, end);
        }
    }
}