Describe how you could use a single array to implement three stacks.
Yout should implement push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum) methods. stackNum is the index of the stack. value is the value that pushed to the stack.
The constructor requires a stackSize parameter, which represents the size of each stack.
Example1:
Input:
["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
Output:
[null, null, null, 1, -1, -1, true]
Explanation: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing.
We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \textit{cap} + 3$ to simulate three stacks. The first $3 \times \textit{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack.
For the push operation, we first check whether the stack is full. If it is not full, we push the element into the stack and update the number of elements in the stack.
For the pop operation, we first check whether the stack is empty. If it is not empty, we update the number of elements in the stack and return the top element of the stack.
For the peek operation, we first check whether the stack is empty. If it is not empty, we return the top element of the stack.
For the isEmpty operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\textit{cap} \times 3 + i]$ is $0$.
In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\textit{cap})$, where $\textit{cap}$ is the size of the stack.
classTripleInOne:def__init__(self,stackSize:int):self.cap=stackSizeself.stk=[0]*(self.cap*3+3)defpush(self,stackNum:int,value:int)->None:ifself.stk[self.cap*3+stackNum]<self.cap:self.stk[self.cap*stackNum+self.stk[self.cap*3+stackNum]]=valueself.stk[self.cap*3+stackNum]+=1defpop(self,stackNum:int)->int:ifself.isEmpty(stackNum):return-1self.stk[self.cap*3+stackNum]-=1returnself.stk[self.cap*stackNum+self.stk[self.cap*3+stackNum]]defpeek(self,stackNum:int)->int:ifself.isEmpty(stackNum):return-1returnself.stk[self.cap*stackNum+self.stk[self.cap*3+stackNum]-1]defisEmpty(self,stackNum:int)->bool:returnself.stk[self.cap*3+stackNum]==0# Your TripleInOne object will be instantiated and called as such:# obj = TripleInOne(stackSize)# obj.push(stackNum,value)# param_2 = obj.pop(stackNum)# param_3 = obj.peek(stackNum)# param_4 = obj.isEmpty(stackNum)
classTripleInOne{privateintcap;privateint[]stk;publicTripleInOne(intstackSize){cap=stackSize;stk=newint[cap*3+3];}publicvoidpush(intstackNum,intvalue){if(stk[cap*3+stackNum]<cap){stk[cap*stackNum+stk[cap*3+stackNum]]=value;++stk[cap*3+stackNum];}}publicintpop(intstackNum){if(isEmpty(stackNum)){return-1;}--stk[cap*3+stackNum];returnstk[cap*stackNum+stk[cap*3+stackNum]];}publicintpeek(intstackNum){returnisEmpty(stackNum)?-1:stk[cap*stackNum+stk[cap*3+stackNum]-1];}publicbooleanisEmpty(intstackNum){returnstk[cap*3+stackNum]==0;}}/** * Your TripleInOne object will be instantiated and called as such: * TripleInOne obj = new TripleInOne(stackSize); * obj.push(stackNum,value); * int param_2 = obj.pop(stackNum); * int param_3 = obj.peek(stackNum); * boolean param_4 = obj.isEmpty(stackNum); */
classTripleInOne{public:TripleInOne(intstackSize){cap=stackSize;stk.resize(cap*3+3);}voidpush(intstackNum,intvalue){if(stk[cap*3+stackNum]<cap){stk[cap*stackNum+stk[cap*3+stackNum]]=value;++stk[cap*3+stackNum];}}intpop(intstackNum){if(isEmpty(stackNum)){return-1;}--stk[cap*3+stackNum];returnstk[cap*stackNum+stk[cap*3+stackNum]];}intpeek(intstackNum){returnisEmpty(stackNum)?-1:stk[cap*stackNum+stk[cap*3+stackNum]-1];}boolisEmpty(intstackNum){returnstk[cap*3+stackNum]==0;}private:intcap;vector<int>stk;};/** * Your TripleInOne object will be instantiated and called as such: * TripleInOne* obj = new TripleInOne(stackSize); * obj->push(stackNum,value); * int param_2 = obj->pop(stackNum); * int param_3 = obj->peek(stackNum); * bool param_4 = obj->isEmpty(stackNum); */
classTripleInOne{privatecap:number;privatestk:number[];constructor(stackSize:number){this.cap=stackSize;this.stk=Array<number>(stackSize*3+3).fill(0);}push(stackNum:number,value:number):void{if(this.stk[this.cap*3+stackNum]<this.cap){this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]]=value;this.stk[this.cap*3+stackNum]++;}}pop(stackNum:number):number{if(this.isEmpty(stackNum)){return-1;}this.stk[this.cap*3+stackNum]--;returnthis.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]];}peek(stackNum:number):number{if(this.isEmpty(stackNum)){return-1;}returnthis.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]-1];}isEmpty(stackNum:number):boolean{returnthis.stk[this.cap*3+stackNum]===0;}}/** * Your TripleInOne object will be instantiated and called as such: * var obj = new TripleInOne(stackSize) * obj.push(stackNum,value) * var param_2 = obj.pop(stackNum) * var param_3 = obj.peek(stackNum) * var param_4 = obj.isEmpty(stackNum) */
classTripleInOne{privatevarcap:Intprivatevarstk:[Int]init(_stackSize:Int){self.cap=stackSizeself.stk=[Int](repeating:0,count:cap*3+3)}funcpush(_stackNum:Int,_value:Int){ifstk[cap*3+stackNum]<cap{stk[cap*stackNum+stk[cap*3+stackNum]]=valuestk[cap*3+stackNum]+=1}}funcpop(_stackNum:Int)->Int{ifisEmpty(stackNum){return-1}stk[cap*3+stackNum]-=1returnstk[cap*stackNum+stk[cap*3+stackNum]]}funcpeek(_stackNum:Int)->Int{ifisEmpty(stackNum){return-1}returnstk[cap*stackNum+stk[cap*3+stackNum]-1]}funcisEmpty(_stackNum:Int)->Bool{returnstk[cap*3+stackNum]==0}}/** * Your TripleInOne object will be instantiated and called as such: * let obj = TripleInOne(stackSize) * obj.push(stackNum,value) * let param_2 = obj.pop(stackNum) * let param_3 = obj.peek(stackNum) * let param_4 = obj.isEmpty(stackNum) */