Skip to content

Instantly share code, notes, and snippets.

@GeorgePaiva
Last active June 24, 2020 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgePaiva/5c22b1df8ca4d097d856464326a462ae to your computer and use it in GitHub Desktop.
Save GeorgePaiva/5c22b1df8ca4d097d856464326a462ae to your computer and use it in GitHub Desktop.
package desafiometa;
public class Questão04 {
static int arr[] = new int[] { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
static int capAgua(int n) {
int esquerda[] = new int[n];
int direita[] = new int[n];
int agua = 0;
esquerda[0] = arr[0];
for (int i = 1; i < n; i++)
esquerda[i] = Math.max(esquerda[i - 1], arr[i]);
direita[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--)
direita[i] = Math.max(direita[i + 1], arr[i]);
for (int i = 0; i < n; i++)
agua += Math.min(esquerda[i], direita[i]) - arr[i];
return agua;
}
public static void main(String[] args) {
System.out.println("A água máxima que pode ser acumulada é " + capAgua(arr.length));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment