-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_plates.js
More file actions
68 lines (59 loc) · 1.29 KB
/
stack_plates.js
File metadata and controls
68 lines (59 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Cracking the Coding Interview Stack And Queues 3.3
//Stack plates with max capacity
class setOfStacks{
constructor(){
this.max = 10
this.stack = [];
}
push(val){
var lastStack = this.stack[this.stack.length-1]
if(this.stack.length == 0 || lastStack.length == this.max){
this.stack.push([val]);
} else{
lastStack.push(val);
}
return this;
}
pop(){
if(this.stack.length == 0){
return this;
}
else{
var lastStack = this.stack[this.stack.length-1]
var temp = lastStack.pop();
if(lastStack.length == 0){
this.stack.pop();
}
return temp;
}
}
popAt(ind){
var stack = Math.floor(ind / (this.max));
var stack_ind = ind % (this.max);
if(stack > this.stack.length){
return this;
}
this.stack[stack].splice(stack_ind, 1);
// if(this.stack[stack].length == 0){
// this.stack.pop();
// }
for(var i = stack+1; i < this.stack.length; i++){
var temp = this.stack[stack+1].splice(0,1);
this.stack[stack].push(temp[0]);
stack++;
}
}
}
var arr = new setOfStacks();
for(var i =0; i < 41; i ++){
arr.push(i);
}
for(var i =0; i < 7; i ++){
arr.pop();
}
arr.popAt(10);
arr.popAt(22);
arr.popAt(31);
arr.popAt(30);
arr.popAt(30);
console.log(arr.stack);