You are given a **0-indexed** array of non-negative integers `nums`. For each integer, you must find its **second greater** integer. The **second greater** integer of `nums[i]` is `nums[j]` such that: - `j > i` - `nums[j] > nums[i]` - There exists **exactly one** index `k` such that `i < k < j` and `nums[k] > nums[i]`. If there is no such `nums[j]`, the second greater integer is considered to be `-1`. Return an integer array `answer`, where `answer[i]` is the second greater integer of `nums[i]`.
Example 1
Input: nums = [2,4,0,9,6]
Output: [9,6,6,-1,-1]
Explanation: For 2 the greaters are 4 then 9, so its second greater is 9; for 4 they are 9 then 6, so 6; and so on.
Example 2
Input: nums = [3,3]
Output: [-1,-1]
Explanation: Neither element is exceeded even once.
Example 3
Input: nums = [1,2,3,4,5]
Output: [3,4,5,-1,-1]
Explanation: Each number is followed by bigger numbers in increasing order, so its second greater number sits two places later: 1 → 3, 2 → 4, 3 → 5. 4 and 5 have fewer than two larger numbers after them, so they get -1.
An index needs to be beaten twice. Keep a monotonic stack of indices still waiting for their FIRST greater. When a value beats them, they've had one greater — move them to a 'pending second' collection. A min-heap (keyed by value) of pending indices lets a new value settle every pending index it now beats.
Run checks all cases above. Submit evaluates all test cases.