Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Another Implementation

gabbygabbylexy

Apr 24, 2025

I find this implementation easier to understand

using System; using System.Collections.Generic; public class Solution { public List<int> nextLargerElement(List<int> arr) { List<int> res = new List<int>(); // ToDo: Write Your Code Here. var stack = new Stack<int>(); var i = arr.Count - 1; while (i > -1) { if (stack.Count > 0 && stack.Peek() <= arr[i]) { stack.Pop(); continue; } res.Insert(0, stack.Count > 0 ? stack.Peek() : -1); stack.Push(arr[i]); --i; } return res; } }

0

0

Comments
Comments

On this page