go: add «1381. Design a Stack With Increment Operation»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-30 21:51:13 +02:00
parent 7116e58b36
commit 3033003e6c
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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
}
}