【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 ...
随机推荐
- 快速安装pycharm,最详细的pycharm安装图文教程
大家都知道python的开发工具Pycharm吧,它是由JetBrains打造的一款Python IDE,它功能强大,已经是python开发者使用最多的编辑工具.首先,它支持多平台(Linux.WIn ...
- C#学习笔记四(LINQ,错误和异常,异步编程,反射元数据和动态编程)
LINQ 1.使用类似的数据库语言来操作集合? 错误和异常 异步编程 1.异步和线程的区别: 多线程和异步操作两者都可以达到避免调用线程阻塞的目的.但是,多线程和异步操作还是有一些区别的.而这些区别造 ...
- 关于glog使用中遇到的问题
项目中需要打log,当初看到glog,觉得google出品应该差不了,而且简单易用,库不是很大,就选择他了. 但是在使用中还真的发现一些不顺手和库设计上的问题,反正和我的使用习惯有点不一样. 设置lo ...
- 意想不到的JavaScript(每日一题2)
问题一: 答案: 解析:
- 源码搭建git,并连接github
一.环境 1.下载源码包 https://mirrors.edge.kernel.org/pub/software/scm/git/ 2.安装编译环境 #yum install gcc gcc-c++ ...
- 微信小程序开发(一)----- 基础知识
1.什么是微信小程序 概念:小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用,体现了“用完即走”的理念,用户不需要关心是否安装太多应用的问题, ...
- 2019JAVA第四次实验报告
JAVA实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019/9/29 评分等级 实验四 类的继承 1.实验目的 掌握类的继承方法: 变量的继承和覆盖,方法的继承.重载和 ...
- hdu3829 二分匹配 最大独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...
- day 03 int bool str (索引,切片) for 循环
基础数类型总览 10203 123 3340 int +- * / 等等 '今天吃了没?' str 存储少量的数据,+ *int 切片, 其他操作方法 True False bool 判断真假 [12 ...
- Codeforce1196_D_F
D RGB Substring 题意 给定一个只含RGB三种字符的字符串,问最少修改多少个字符,能使得修改后的字符串存在一个长度为\(k\)的子串是...RGBRGB...这个循环字符串的子串. 分析 ...