Monotonic Stack Problem

Problem Representative monotonic-stack task for this round: Next Greater Element — for each array element, find the first element to its right that is strictly greater (-1 if none). The round uses a problem of this shape, so master the pattern.

Input / Output

  • Input: int array nums.
  • Output: int array of next-greater values (or indices/distances, per variant).

Constraints

  • n up to 10^5 — the O(n^2) per-element scan is the baseline to beat; O(n) expected.

Example

  • [2,1,2,4,3] -> [4,2,4,-1,-1].
  • Distances variant (Daily Temperatures): [73,74,75,71,69,72] -> [1,1,3,2,1,-1].
asked …
LeaderboardSalaryAccount