Matej Focko
17817b1fe9
URL: https://leetcode.com/problems/maximum-width-ramp/ Signed-off-by: Matej Focko <me@mfocko.xyz>
21 lines
371 B
Go
21 lines
371 B
Go
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
|
|
}
|