diff --git a/datastructure/src/main/java/org/alis/algorithm/leetcode/Leetcode19.java b/datastructure/src/main/java/org/alis/algorithm/leetcode/Leetcode19.java new file mode 100644 index 0000000..5e63cef --- /dev/null +++ b/datastructure/src/main/java/org/alis/algorithm/leetcode/Leetcode19.java @@ -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; + } + +} \ No newline at end of file