592.Push onto a Stack with a Capacity Limit
Easy
Push each value of
values, in order, onto an empty stack that can hold at most capacity items. A push onto a full stack is ignored. Return the stack's contents from bottom to top.
A stackStackA last-in, first-out (LIFO) collection: items are added and removed only at the top. only lets you work at one end, the top. PushPushAdding an item to the top of a stack. adds there — but a stack stored in a fixed-size array can run out of room, and pushing anyway is a stack overflowStack OverflowTrying to push onto a stack that is already full.. Checking for space before every push is the core habit this exercise practises.
Example 1:
Input: values = [4,7,1,9], capacity = 3
Output: [4,7,1]
Example 2:
Input: values = [5,6], capacity = 4
Output: [5,6]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ values.length ≤ 1000 - ●
1 ≤ capacity ≤ 1000 - ●
-1000000 ≤ values[i] ≤ 1000000
values =
[4, 7, 1, 9]
capacity =
3