go: add «962. Maximum Width Ramp»

URL:	https://leetcode.com/problems/maximum-width-ramp/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-10 21:16:14 +02:00
parent f57f51a287
commit 17817b1fe9
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

21
go/maximum-width-ramp.go Normal file
View file

@ -0,0 +1,21 @@
package main
func maxWidthRamp(nums []int) int {
st := make([]int, 0)
for i, x := range nums {
if len(st) == 0 || nums[st[len(st)-1]] > x {
st = append(st, i)
}
}
widest := 0
for j := len(nums) - 1; j >= 0; j-- {
for len(st) > 0 && nums[st[len(st)-1]] <= nums[j] {
widest = max(widest, j-st[len(st)-1])
st = st[:len(st)-1]
}
}
return widest
}