【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
DinnerPlates
class:
DinnerPlates(int capacity)
Initializes the object with the maximumcapacity
of the stacks.void push(int val)
pushes the given positive integerval
into 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-1
if all stacks are empty.int popAtStack(int index)
returns the value at the top of the stack with the givenindex
and removes it from that stack, and returns -1 if the stack with that givenindex
is 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 <= 20000
1 <= val <= 20000
0 <= index <= 100000
- At most
200000
calls 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 ...
随机推荐
- 使用Jquery的Ajax调用
最近在学习web开发,试用了一下Jquery的ajax调用. 首先,新建一个MVC4的项目,在HomeController.cs中添加一个Action,命名为GetData, 通过这个为ajax提供数 ...
- 如何利用Prometheus监控你的应用
Prometheus作为一套完整的开源监控接近方案,因为其诸多强大的特性以及生态的开放性,俨然已经成为了监控领域的事实标准并在全球范围内得到了广泛的部署应用.那么应该如何利用Prometheus对我们 ...
- 【LeetCode】122、买卖股票的最佳时机 II
Best Time to Buy and Sell Stock II 题目等级:Easy 题目描述: Say you have an array for which the ith element i ...
- 【神经网络与深度学习】【Qt开发】【VS开发】从caffe-windows-visual studio2013到Qt5.7使用caffemodel进行分类的移植过程
[神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置成功后的第一次训练过程记录<二> 标签:[神经网络与深度学习] [CUDA开发] ...
- xampp:windows找不到文件“-n”
转自:http://blog.csdn.net/soar92/article/details/72897789 安装xampp是总是出总是提示以下错误: ①安装xampp时提示windows找不到文件 ...
- PostgreSQL INSERT ON CONFLICT不存在则插入,存在则更新
近期有一个需求,向一张数据库表插入数据,如果是新数据则执行插入动作,如果插入的字段和已有字段重复,则更新该行对应的部分字段 1. 创建测试表 create table meta_data ( id s ...
- 利用BFS解决拯救007问题 -- 数据结构
题目: 7-1 拯救007 (30 分) 在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接 ...
- gcc 数据对齐之:总结篇.
通过上面的分析,总结结构体对齐规则如下: 1.数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragm ...
- Balloon Robot ZOJ - 3981
大意: n个参赛队, m个座位, 一共交了p次题, 一个机器人每秒钟会从位置$i$走到$i+1$, 若在$m$直接走到$1$, 当走到一个队伍就给该队应得的气球. 对于每道题, 假设交题时间$t_a$ ...
- 使用 tablib 来自动化管理测试用例,其他的工具都不用学了
你在学习 python 自动化测试吗?听过 requests 库吗?tablib 是 requests 库作者常年维护的一个可以操作 Excel 等多种文件格式,将他们变成一种通用数据集的第三方库. ...