Last active
June 24, 2020 13:51
-
-
Save GeorgePaiva/5c22b1df8ca4d097d856464326a462ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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