mediumArraysPattern: Stack LIFO
Priority Crate Management Solution
Problem Statement
Implement a CrateManager class that utilizes a stack data structure to manage a collection of crates, where each crate is represented by its value. The class must support three primary operations: addCrate, which adds a crate to the top of the stack; removeCrate, which removes the top crate from the stack; and peekHighestPriority, which returns the value of the highest-priority crate without removing it, with priority determined by the crate's value in descending order.
Examples
Example 1:
Input:["addCrate(5)","addCrate(3)","peekHighestPriority()","removeCrate()","peekHighestPriority()"]
Output:[null,null,5,3,3]
Explanation: Corrected output for the last call to peekHighestPriority() after removing the highest priority crate
Constraints
- At most 10^4 crates are added to or removed from the inventory.
- The value of each crate is between 1 and 10^5.
Time: O(1) Space: O(n)
The optimized approach involves using a stack to manage the inventory and keeping track of the highest priority crate, allowing for efficient retrieval with a time complexity of O(1).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
