comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
给定一个整数数组 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.
提示:
2 <= A.length <= 50000
0 <= A[i] <= 50000
根据题意,我们可以发现,所有可能的
假设存在
我们用一个从栈底到栈顶单调递减的栈
时间复杂度
class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
stk = []
for i, v in enumerate(nums):
if not stk or nums[stk[-1]] > v:
stk.append(i)
ans = 0
for i in range(len(nums) - 1, -1, -1):
while stk and nums[stk[-1]] <= nums[i]:
ans = max(ans, i - stk.pop())
if not stk:
break
return ans
class Solution {
public int maxWidthRamp(int[] nums) {
int n = nums.length;
Deque<Integer> stk = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (stk.isEmpty() || nums[stk.peek()] > nums[i]) {
stk.push(i);
}
}
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) {
ans = Math.max(ans, i - stk.pop());
}
if (stk.isEmpty()) {
break;
}
}
return ans;
}
}
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
int n = nums.size();
stack<int> stk;
for (int i = 0; i < n; ++i) {
if (stk.empty() || nums[stk.top()] > nums[i]) stk.push(i);
}
int ans = 0;
for (int i = n - 1; i; --i) {
while (!stk.empty() && nums[stk.top()] <= nums[i]) {
ans = max(ans, i - stk.top());
stk.pop();
}
if (stk.empty()) break;
}
return ans;
}
};
func maxWidthRamp(nums []int) int {
n := len(nums)
stk := []int{}
for i, v := range nums {
if len(stk) == 0 || nums[stk[len(stk)-1]] > v {
stk = append(stk, i)
}
}
ans := 0
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && nums[stk[len(stk)-1]] <= nums[i] {
ans = max(ans, i-stk[len(stk)-1])
stk = stk[:len(stk)-1]
}
if len(stk) == 0 {
break
}
}
return ans
}
function maxWidthRamp(nums: number[]): number {
let [ans, n] = [0, nums.length];
const stk: number[] = [];
for (let i = 0; i < n - 1; i++) {
if (stk.length === 0 || nums[stk.at(-1)!] > nums[i]) {
stk.push(i);
}
}
for (let i = n - 1; i >= 0; i--) {
while (stk.length && nums[stk.at(-1)!] <= nums[i]) {
ans = Math.max(ans, i - stk.pop()!);
}
if (stk.length === 0) break;
}
return ans;
}
function maxWidthRamp(nums) {
let [ans, n] = [0, nums.length];
const stk = [];
for (let i = 0; i < n - 1; i++) {
if (stk.length === 0 || nums[stk.at(-1)] > nums[i]) {
stk.push(i);
}
}
for (let i = n - 1; i >= 0; i--) {
while (stk.length && nums[stk.at(-1)] <= nums[i]) {
ans = Math.max(ans, i - stk.pop());
}
if (stk.length === 0) break;
}
return ans;
}
function maxWidthRamp(nums: number[]): number {
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
let [ans, j] = [0, nums.length];
for (const [_, i] of idx) {
ans = Math.max(ans, i - j);
j = Math.min(j, i);
}
return ans;
}
function maxWidthRamp(nums) {
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
let [ans, j] = [0, nums.length];
for (const [_, i] of idx) {
ans = Math.max(ans, i - j);
j = Math.min(j, i);
}
return ans;
}