2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates, Di

2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号,

每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates,

DinnerPlates(int capacity) - 给出栈的最大容量 capacity,

void push(int val) 将给出的正整数 val 推入 从左往右第一个 没有满的栈,

int pop() 返回 从右往左第一个 非空栈顶部的值,并将其从栈中删除,

如果所有的栈都是空的,请返回 -1。

int popAtStack(int index) - 返回编号 index 的栈顶部的值,并将其从栈中删除,

如果编号 index 的栈是空的,请返回 -1。

输入:["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"][[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]]。

输出:[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1]。

答案2024-03-09:

来自左程云

灵捷3.5

大体步骤如下:

这个问题需要实现一个类 DinnerPlates,其中含有 ConstructorPushPopPopAtStack 四个方法。这个类可以理解成是具有固定容量的多个栈构成的一种数据结构。根据题目描述和提供的 Go 代码文件,这里来分步骤描述大体过程,然后讨论总的时间复杂度和总的空间复杂度。

1.Constructor:

  • 当创建 DinnerPlates 实例时,通过调用 Constructor 方法初始化一个 DinnerPlates 类型的实例。需要传入一个参数 capacity 表示栈的最大容量。在这个方法中,将 capacity 存储到实例字段中,并初始化 stacktoppoppedPos 三个切片。

2.Push:

  • 当调用 Push 方法时,将给定的整数值 val 推入从左到右第一个没有满的栈。

  • 如果所有栈都已满,应该创建一个新的栈来存储 val

  • 如果有栈未满,则将 val 推入最左侧未满的栈中,并更新 top 数组和 stack 数组。

3.Pop:

  • 当调用 Pop 方法时,应该返回最右侧非空栈顶的值,并将其从栈中删除。如果所有的栈都为空,返回 -1。

  • 如果有非空的栈,应该找到最右侧非空栈并返回它的栈顶的值,然后将其值从栈中删除。

4.PopAtStack:

  • 当调用 PopAtStack 方法时,应该返回指定 index 栈的栈顶的值,并将其从栈中删除。如果指定 index 的栈为空,返回 -1。

  • 需要更新 top 数组和 poppedPos 数组,以确保栈的一致性。

总的时间复杂度:

  • Push 方法的时间复杂度为 O(1)。

  • Pop 方法的时间复杂度为 O(1)。

  • PopAtStack 方法的时间复杂度为 O(log n),其中 n 是被删除的元素的数量。

总的空间复杂度:

  • 需要 O(n) 的空间来存储栈中的所有元素,其中 n 是所有栈的元素数量。

go完整代码如下:

package main

import (
	"fmt"
	"sort"
)

type DinnerPlates struct {
	capacity  int
	stack     []int
	top       []int
	poppedPos []int
}

func Constructor(capacity int) DinnerPlates {
	return DinnerPlates{capacity, []int{}, []int{}, []int{}}
}

func (this *DinnerPlates) Push(val int) {
	if len(this.poppedPos) == 0 {
		pos := len(this.stack)
		this.stack = append(this.stack, val)
		if pos%this.capacity == 0 {
			this.top = append(this.top, 0)
		} else {
			stackPos := len(this.top) - 1
			stackTop := this.top[stackPos]
			this.top[stackPos] = stackTop + 1
		}
	} else {
		pos := this.poppedPos[0]
		this.poppedPos = this.poppedPos[1:]
		this.stack[pos] = val
		index := pos / this.capacity
		stackTop := this.top[index]
		this.top[index] = stackTop + 1
	}
}

func (this *DinnerPlates) Pop() int {
	for len(this.stack) > 0 && len(this.poppedPos) > 0 && this.poppedPos[len(this.poppedPos)-1] == len(this.stack)-1 {
		this.stack = this.stack[:len(this.stack)-1]
		pos := this.poppedPos[len(this.poppedPos)-1]
		this.poppedPos = this.poppedPos[:len(this.poppedPos)-1]
		if pos%this.capacity == 0 {
			this.top = this.top[:len(this.top)-1]
		}
	}
	if len(this.stack) == 0 {
		return -1
	} else {
		pos := len(this.stack) - 1
		val := this.stack[pos]
		this.stack = this.stack[:pos]
		if pos%this.capacity == 0 && len(this.top) > 0 {
			this.top = this.top[:len(this.top)-1]
		} else if len(this.top) > 0 {
			this.top[len(this.top)-1] -= 1
		}
		return val
	}
}

func (this *DinnerPlates) PopAtStack(index int) int {
	if index >= len(this.top) {
		return -1
	}
	stackTop := this.top[index]
	if stackTop < 0 {
		return -1
	}
	this.top[index] = stackTop - 1
	pos := index*this.capacity + stackTop
	i := sort.SearchInts(this.poppedPos, pos)
	this.poppedPos = append(this.poppedPos, 0)
	copy(this.poppedPos[i+1:], this.poppedPos[i:])
	this.poppedPos[i] = pos
	return this.stack[pos]
}

func main() {
	dinnerPlates := Constructor(2)
	dinnerPlates.Push(1)
	dinnerPlates.Push(2)
	dinnerPlates.Push(3)
	dinnerPlates.Push(4)
	dinnerPlates.Push(5)
	fmt.Println(dinnerPlates.PopAtStack(0))
	dinnerPlates.Push(20)
	dinnerPlates.Push(21)
	fmt.Println(dinnerPlates.PopAtStack(0))
	fmt.Println(dinnerPlates.PopAtStack(2))
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
}

在这里插入图片描述

posted @   福大大架构师每日一题  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
历史上的今天:
2023-03-09 2023-03-09:用golang调用ffmpeg,将流媒体数据(以RTMP为例)保存成本地文件(以flv为例)。
2022-03-09 2022-03-09:我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字。 你来猜我选了哪个数字。 如果你猜到正确的数字,就会 赢得游戏 。 如果你猜错了,那么我会告诉你,我选
2021-03-09 2021-03-09:在一个数组中,一个数左边比它小的数的总和,叫数的小和,所有数的小和累加起来,叫数组小和。求数组小和。例子: [1,3,4,2,5],1左边比1小的数:没有,3左边比3小的数:1,4左边比4小的数:1、3,2左边比2小的数:1,5左边比5小的数:1、3、4、 2,所以数组的小和为1+1+3+1+1+3+4+2=16 。
2021-03-09 2021-03-09:在一个数组中,一个数左边比它小的数的总和,叫数的小和,所有数的小和累加起来,叫数组小和。求数组小和。 例子: [1,3,4,2,5],1左边比1小的数:没有,3左边比3小的数:1
点击右上角即可分享
微信分享提示