Untitled
unknown
plain_text
3 years ago
1.4 kB
9
Indexable
public class Question5 {
public static void main(String[] args) {
// int[] a = {3, 0, 2, 0, 4};
int[] a = {1, 2, 0, 3, 1, 0, 1, 2, 2, 1, 2, 0, 1};
int totalFilling = 0;
int fillingHeight;
for (int i = 0; i + 1 < a.length; ) {
int nextStop = 0;
fillingHeight = a[i];
for (int j = i + 1; j < a.length; j++) {
if (a[j] >= a[i]) {
nextStop = j;
break;
}
}
if (nextStop == 0) {
int max = 0;
int maxIndex = 0;
for (int j = i + 1; j < a.length; j++) {
if (a[j] >= max) {
max = a[j];
maxIndex = j;
}
}
nextStop = maxIndex;
fillingHeight = max;
}
if (i == 0 && a[i] < a[i + 1]) {
i = nextStop;
continue;
}
for (int j = i + 1; j < nextStop; j++) {
int filling = fillingHeight - a[j];
a[j] += filling;
totalFilling += filling;
}
i = nextStop;
}
System.out.println(totalFilling);
System.out.println(Arrays.toString(a));
}
}Editor is loading...