Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 42f75a8

Browse files
committedApr 5, 2025
Add solution #1172
1 parent 82558c4 commit 42f75a8

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,165 LeetCode solutions in JavaScript
1+
# 1,166 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -913,6 +913,7 @@
913913
1169|[Invalid Transactions](./solutions/1169-invalid-transactions.js)|Medium|
914914
1170|[Compare Strings by Frequency of the Smallest Character](./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js)|Medium|
915915
1171|[Remove Zero Sum Consecutive Nodes from Linked List](./solutions/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js)|Medium|
916+
1172|[Dinner Plate Stacks](./solutions/1172-dinner-plate-stacks.js)|Hard|
916917
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
917918
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
918919
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|

‎solutions/1172-dinner-plate-stacks.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* 1172. Dinner Plate Stacks
3+
* https://leetcode.com/problems/dinner-plate-stacks/
4+
* Difficulty: Hard
5+
*
6+
* You have an infinite number of stacks arranged in a row and numbered (left to right) from 0,
7+
* each of the stacks has the same maximum capacity.
8+
*
9+
* Implement the DinnerPlates class:
10+
* - DinnerPlates(int capacity) Initializes the object with the maximum capacity of the stacks
11+
* capacity.
12+
* - void push(int val) Pushes the given integer val into the leftmost stack with a size less
13+
* than capacity.
14+
* - int pop() Returns the value at the top of the rightmost non-empty stack and removes it
15+
* from that stack, and returns -1 if all the stacks are empty.
16+
* - int popAtStack(int index) Returns the value at the top of the stack with the given index
17+
* index and removes it from that stack or returns -1 if the stack with that given index is empty.
18+
*/
19+
20+
/**
21+
* @param {number} capacity
22+
*/
23+
var DinnerPlates = function(capacity) {
24+
this.maxCapacity = capacity;
25+
this.plateStacks = [];
26+
this.availableIndices = [];
27+
};
28+
29+
/**
30+
* @param {number} val
31+
* @return {void}
32+
*/
33+
DinnerPlates.prototype.push = function(val) {
34+
const targetIndex = this.availableIndices.length
35+
? this.availableIndices.pop()
36+
: this.plateStacks.length - 1;
37+
38+
if (!this.plateStacks[targetIndex] || this.plateStacks[targetIndex].length === this.maxCapacity) {
39+
this.plateStacks.push([val]);
40+
} else {
41+
this.plateStacks[targetIndex].push(val);
42+
}
43+
};
44+
45+
/**
46+
* @return {number}
47+
*/
48+
DinnerPlates.prototype.pop = function() {
49+
while (this.plateStacks.length && !this.plateStacks.at(-1).length) {
50+
while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) {
51+
this.availableIndices.pop();
52+
}
53+
this.plateStacks.pop();
54+
}
55+
56+
if (!this.plateStacks.length) {
57+
return -1;
58+
}
59+
60+
const result = this.plateStacks.at(-1).pop() ?? -1;
61+
62+
while (this.plateStacks.length && !this.plateStacks.at(-1).length) {
63+
while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) {
64+
this.availableIndices.pop();
65+
}
66+
this.plateStacks.pop();
67+
}
68+
69+
return result;
70+
};
71+
72+
/**
73+
* @param {number} index
74+
* @return {number}
75+
*/
76+
DinnerPlates.prototype.popAtStack = function(index) {
77+
if (index >= this.plateStacks.length || !this.plateStacks[index].length) {
78+
return -1;
79+
}
80+
81+
const result = this.plateStacks[index].pop();
82+
const temp = [];
83+
84+
while (this.availableIndices.length && this.availableIndices.at(-1) < index) {
85+
temp.push(this.availableIndices.pop());
86+
}
87+
88+
this.availableIndices.push(index);
89+
while (temp.length) {
90+
this.availableIndices.push(temp.shift());
91+
}
92+
93+
return result;
94+
};

0 commit comments

Comments
 (0)
Please sign in to comment.