You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class_2022_10_3_week;
// 给定一个整数数组 A坡是元组 (i, j)其中  i < j  A[i] <= A[j]
// 这样的坡的宽度为 j - i
// 找出 A 中的坡的最大宽度如果不存在返回 0
// 示例 1
// 输入:[6,0,8,2,1,5]
// 输出4
// 解释:
// 最大宽度的坡为 (i, j) = (1, 5): A[1] = 0 且 A[5] = 5
// 示例 2
// 输入:[9,8,1,0,1,9,4,0,4,1]
// 输出7
// 解释:
// 最大宽度的坡为 (i, j) = (2, 9): A[2] = 1 且 A[9] = 1
// 测试链接 : https://leetcode.cn/problems/maximum-width-ramp/
public class Code01_MaximumWidthRamp {
public static int maxWidthRamp(int[] arr) {
int n = arr.length;
// 栈中只放下标
int[] stack = new int[n];
// 栈的大小
int r = 0;
for (int i = 0; i < n; i++) {
if (r == 0 || arr[stack[r - 1]] > arr[i]) {
stack[r++] = i;
}
}
int ans = 0;
// 从右往左遍历
// j = n - 1
for (int j = n - 1; j >= 0; j--) {
while (r != 0 && arr[stack[r - 1]] <= arr[j]) {
int i = stack[--r];
ans = Math.max(ans, j - i);
}
}
return ans;
}
}