From 3033003e6cc43517275870c0453619dc86352c75 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 30 Sep 2024 21:51:13 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1381.=20Design=20a=20Stack=20?= =?UTF-8?q?With=20Increment=20Operation=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/design-a-stack-with-increment-operation.go | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 go/design-a-stack-with-increment-operation.go diff --git a/go/design-a-stack-with-increment-operation.go b/go/design-a-stack-with-increment-operation.go new file mode 100644 index 0000000..30f26ab --- /dev/null +++ b/go/design-a-stack-with-increment-operation.go @@ -0,0 +1,39 @@ +package main + +type CustomStack struct { + maxSize int + stack []int + size int +} + +func Constructor(maxSize int) CustomStack { + return CustomStack{ + maxSize: maxSize, + stack: make([]int, maxSize), + size: 0, + } +} + +func (this *CustomStack) Push(x int) { + if this.size >= this.maxSize { + return + } + + this.stack[this.size] = x + this.size++ +} + +func (this *CustomStack) Pop() int { + if this.size <= 0 { + return -1 + } + + this.size-- + return this.stack[this.size] +} + +func (this *CustomStack) Increment(k int, val int) { + for i := 0; i < k && i < this.size; i++ { + this.stack[i] += val + } +}