1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-10-18 06:42:08 +02:00
LeetCode/go/maximum-width-ramp.go

22 lines
371 B
Go
Raw Permalink Normal View History

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
}