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:
parent
f57f51a287
commit
17817b1fe9
1 changed files with 21 additions and 0 deletions
21
go/maximum-width-ramp.go
Normal file
21
go/maximum-width-ramp.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue