We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Stacks
- Maximum Element
- Discussions
Maximum Element
Maximum Element
Sort by
recency
|
1461 Discussions
|
Please Login in order to post a comment
public static List getMax(List operations) { // Write your code here List list = new ArrayList<>();
We keep the newer elements to be appended to stack as the maximum element since you'll need only the maximum element
Java 8 Solution
function getMax(operations) { let arr = [], results = []; operations.forEach(element => { if (element.startsWith("1")) { arr.push(element.split(" ")[1]) } else if (element.startsWith("2") && arr.length !== 0) { arr.pop() } else if (element.startsWith("3")) { let max = Math.max(...arr); results.push(max); } }); return results; }