365. Next Greater Element IV

HardStack

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]`.

Examples

Input: [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.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Loading...

Run checks all cases above. Submit evaluates all test cases.