【leetcode】1172. Dinner Plate Stacks
题目如下:
You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum
capacity.Implement the
DinnerPlatesclass:
DinnerPlates(int capacity)Initializes the object with the maximumcapacityof the stacks.void push(int val)pushes the given positive integervalinto the leftmost stack with size less thancapacity.int pop()returns the value at the top of the rightmost non-empty stack and removes it from that stack, and returns-1if all stacks are empty.int popAtStack(int index)returns the value at the top of the stack with the givenindexand removes it from that stack, and returns -1 if the stack with that givenindexis empty.Example:
Input:
["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],[],[],[],[],[]]
Output:
[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1] Explanation:
DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2
D.push(1);
D.push(2);
D.push(3);
D.push(4);
D.push(5); // The stacks are now: 2 4
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // Returns 2. The stacks are now: 4
1 3 5
﹈ ﹈ ﹈
D.push(20); // The stacks are now: 20 4
1 3 5
﹈ ﹈ ﹈
D.push(21); // The stacks are now: 20 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // Returns 20. The stacks are now: 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(2); // Returns 21. The stacks are now: 4
1 3 5
﹈ ﹈ ﹈
D.pop() // Returns 5. The stacks are now: 4
1 3
﹈ ﹈
D.pop() // Returns 4. The stacks are now: 1 3
﹈ ﹈
D.pop() // Returns 3. The stacks are now: 1
﹈
D.pop() // Returns 1. There are no stacks.
D.pop() // Returns -1. There are still no stacks.Constraints:
1 <= capacity <= 200001 <= val <= 200000 <= index <= 100000- At most
200000calls will be made topush,pop, andpopAtStack.
解题思路:本题我用了三个list,一个是stack_list,保存所以的stack信息;一个是nonEmptyStack,记录当前不为空的stack的下标;还一个是availableStack,记录当前未满的stack的下标。在push的时候,只需要找出availableStack中所有下标的最小值,插入对应的stack即可,如果availableStack为空,新增一个stack,插入stack_list,同时更新availableStack和nonEmptyStack的状态;pop操作则是找出nonEmptyStack中下标的最大值,对其对应的stack做pop操作,而popAsStack的操作就更简单,可以直接用下标访问stack_list。需要注意的是,每次对stack有任何操作,都要同步更新availableStack和nonEmptyStack。因为是求availableStack和nonEmptyStack的最大或者最小值,只需要保证availableStack和nonEmptyStack中的元素有序即可,更新availableStack和nonEmptyStack则可以用二分查找法。
代码如下:
class DinnerPlates(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.stack_list = []
self.availableStack = []
self.capacity = capacity
self.nonEmptyStack = []
def push(self, val):
"""
:type val: int
:rtype: None
"""
if len(self.availableStack) == 0:
inx = len(self.stack_list)
#self.availableStack.append(len(self.stack_list))
self.stack_list.append([val])
if len(self.stack_list[inx]) < self.capacity:
self.availableStack.append(inx)
else:
inx = self.availableStack[0]
self.stack_list[inx].append(val)
if len(self.stack_list[inx]) >= self.capacity:
self.availableStack.pop(0)
import bisect
b_inx = bisect.bisect_left(self.nonEmptyStack,inx)
if b_inx == -1 or b_inx == len(self.nonEmptyStack) or self.nonEmptyStack[b_inx] != inx:
bisect.insort_left(self.nonEmptyStack,inx)
def pop(self):
"""
:rtype: int
"""
if len(self.nonEmptyStack) == 0:
return -1
inx = self.nonEmptyStack[-1]
v = self.stack_list[inx].pop(-1)
if len(self.stack_list[inx]) == 0:
self.nonEmptyStack.pop(-1)
return v
def popAtStack(self, index):
"""
:type index: int
:rtype: int
"""
import bisect
b_inx = bisect.bisect_left(self.nonEmptyStack, index)
if b_inx == -1 or b_inx == len(self.nonEmptyStack) or self.nonEmptyStack[b_inx] != index:
return -1
v = self.stack_list[index].pop(-1)
if len(self.stack_list[index]) == 0:
del self.nonEmptyStack[b_inx]
b_inx = bisect.bisect_left(self.availableStack, index)
if b_inx == -1 or b_inx == len(self.availableStack) or self.availableStack[b_inx] != index:
bisect.insort_left(self.availableStack,index)
return v
【leetcode】1172. Dinner Plate Stacks的更多相关文章
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 第 10 章 python进程与多进程
一.背景知识 顾明思义,进程即正在执行的一个过程,进程是对正在云的程序的一个抽象. 进程的概念起源与操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一,操作系统的其他所 ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- 【转载】ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
转载出处 在网上下载了一个免安装包的MySQL,准备自己create database jhp_test,使用的时候出现报错,如下: ERROR (): Access denied for user ...
- 奉献pytorch 搭建 CNN 卷积神经网络训练图像识别的模型,配合numpy 和matplotlib 一起使用调用 cuda GPU进行加速训练
1.Torch构建简单的模型 # coding:utf-8 import torch class Net(torch.nn.Module): def __init__(self,img_rgb=3,i ...
- 【VS开发】动态添加的ActiveX控件如何响应事件
http://blog.csdn.net/xiaoqiqixiao/article/details/574542 今天在csdn上看到一朋友问如何响应动态添加的控件的事件,搜索资料,发现对于一般的应用 ...
- dos2unix Linux解决编写脚本出现“%0D
## Linux解决编写脚本出现“%0D”# 安装# yum install -y dos2unix# 然后进行转化一下脚本,将其中的install_mysql.sh换成你的脚本# dos2unix ...
- MSF魔鬼训练营-3.1.1信息收集-通过DNS和IP地址挖掘目标网络信息
情报搜集环境站渗透测试全过程的80%~90% 一.外围信息搜集(公开渠道信息搜集OSINT open source intelligence) 3.1.1信息收集-通过DNS和IP地址挖掘目标网 ...
- CDH开启ldap
参考: 官网ldap: https://www.cloudera.com/documentation/enterprise/6/6.2/topics/cm_sg_ldap_grp_mappings.h ...
- hive udf编程教程
hive udf编程教程 https://blog.csdn.net/u010376788/article/details/50532166
- Excel透视表进阶之计算字段、计算项、切片器、页面布局
计算字段 在透视表的字段列表中通过函数.公式等方式构建一个新的字段 又称虚拟字段,因为计算字段不会出现在数据源中,对于普通字段的操作,都可以对计算字段进行操作 计算字段只能出现在值区域,不能出现在筛选 ...