最大水池容量

master
alis-lc 12 months ago
parent 990d5e6aa7
commit 0bdb7eb042

@ -0,0 +1,54 @@
package org.alis.algorithm.leetcode;
public class Leetcode19 {
public static void main(String[] args) {
int[] array = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
System.out.println(iterator(array));
}
public int area(int[] array) {
int maxValue = 0;
int right = array.length - 1;
int left = 0;
int l = 0;
int r = right;
while (right > left) {
int len = Math.min(array[left], array[right]);
int area = len * (right - left);
if (maxValue < area) {
maxValue = area;
r = right;
l = left;
}
left++;
}
System.out.printf("left: %d right: %d\n", l, r);
return maxValue;
}
public int iterator(int[] array) {
int maxValue = 0;
int left = 0;
int right = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = array.length - 1; j > i; j--) {
int area = Math.min(array[j], array[i]) * (j - i);
if (maxValue < area) {
maxValue = area;
left = i;
right = j;
}
}
}
System.out.printf("left: %d right: %d\n", left, right);
return maxValue;
}
}
Loading…
Cancel
Save