From 17817b1fe93c6fcd24e5c66e537cbd25f59a0001 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 10 Oct 2024 21:16:14 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB962.=20Maximum=20Width=20Ramp?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/maximum-width-ramp/ Signed-off-by: Matej Focko --- go/maximum-width-ramp.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 go/maximum-width-ramp.go diff --git a/go/maximum-width-ramp.go b/go/maximum-width-ramp.go new file mode 100644 index 0000000..df6d797 --- /dev/null +++ b/go/maximum-width-ramp.go @@ -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 +}