【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 ...
随机推荐
- 【HANA系列】SAP HANA使用XS和HTTP创建proxy
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA使用XS和HTT ...
- LeetCode.1046-最后的石头重量(Last Stone Weight)
这是小川的第388次更新,第418篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第250题(顺位题号是1046).有一个石头集合,每个石头都有一个正整数重量值. 每次,我 ...
- cmd命令简单别木马的蛛丝马迹
一些基本的Windows命令往往可以识别木马的蛛丝马迹,而且在保护网络安全上起到很大的作用. 检测网络连接 如果你怀疑自己的计算机上被别人安装了木马,或者是中了病毒,但是手里没有完善的工具来检测是不是 ...
- 从“int中提取高八位”开始的学习
今天有个学弟问了一个问题,怎么提取int中的高八位. 这个是个非常基础的问题,随便用位运算瞎搞几下就出来了. 看到这个问题的时候,也不知道我当初想了些啥,想了个骚操作,用memcpy把int放到字符串 ...
- Magic Potion(网络流)
原题链接 2018南京的铜牌题,听说学长他们上来就A了,我这个图论选手也就上手做了做,结果一言难尽...... 发此篇博客希望自己能牢记自己的菜... 本题大意:有n个heros和m个monsters ...
- MyBatis一级缓存的笔记及记录
精髓内容来源于<图灵学院> 一.概述: 一级缓存是MyBatis天然自带的,是默认开启且没有关闭的地方,1级缓存只能作用于查询回话中,所以也叫会话缓存: 这里举个例子: 订单表存在一对多的 ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- shell with hadoop
shell 命令操作 hadoop 之前多少提及过,这里做个总结. shell with hdfs 基本命令 bin/hadoop fs 大于下面的命令 bin/hdfs dfs dfs 是 fs 的 ...
- docker数据卷挂载
docker数据卷挂载笔记 我们的服务运行时必不可少的会产生一些日志,或是我们需要把容器内的数据进行备份,甚至多个容器之间进行数据共享,这必然涉及容器的数据管理操作. 容器中管理数据主要有两种方式: ...
- java中<<,>>和>>>的含义
<<,>>,>>>为java中的移位运算符. <<表示左移运算符 例如8<<2,表示将8向左移2位,结果为32.低位补0. 二进制演算 ...