From 0bdb7eb04230c7344cbda8e0ca2090e372f909f1 Mon Sep 17 00:00:00 2001 From: alis-lc Date: Tue, 5 Dec 2023 09:04:12 +0000 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=9C=80=E5=A4=A7=E6=B0=B4?= =?UTF-8?q?=E6=B1=A0=E5=AE=B9=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alis/algorithm/leetcode/Leetcode19.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 datastructure/src/main/java/org/alis/algorithm/leetcode/Leetcode19.java 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