Next Greater Element (Monotonic Stack)

medium
Unit 16 · Backtracking0/3 to milestone

For each element, find the next element to its right that's strictly greater.

Implement:

  • next_greater(nums): a list where entry i is the next greater value, or -1
  • daily_temperatures(temps): for each day, how many days until a warmer one (0 if never)
  • next_greater_circular(nums): like next_greater, but the array wraps around

All three should run in O(n) using a stack of indices.

#stacks#arrays2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Next Greater Element (Monotonic Stack) · TechGradify