Stack (incl. Monotonic)

Use for parentheses/validity, next greater/smaller, histogram/areas.

What this pattern is

Use a LIFO stack for bracket validation and parsing; use a monotonic stack to find next greater/smaller elements and compute range-based metrics in O(n).

Exercise: isValidParens

Task: Given a string s containing ()[]{}, determine if the brackets are well-formed.
Example: "()[]{}" → true, "(]" → false, "([)]" → false
Edge cases: odd length, other chars ignored/forbidden.

Your Solution

Exercise: nextGreater

Task: For each element, find the next greater element to its right (or -1 if none).
Example: [2,1,2,4,3] → [4,2,4,-1,-1]
Edge cases: non-increasing arrays, duplicates, large input.

Your Solution