Next Greater Element Distance (Daily Temperatures) - aloalgo

Next Greater Element Distance (Daily Temperatures)

Medium

You are given an array of numbers, nums.

Your task is to determine, for each element in nums, the number of steps to the right required to find the first element strictly greater than it.

Return an array of integers. For each element in the original nums array, the corresponding element in the returned array should represent the count of steps. If no such strictly greater element exists to its right, use -1.

Example 1

Input
[20, 21, 22, 23]
Output
[1, 1, 1, 0]
Explanation:

At each next the following number is larger, therefore we return an array of 1 for each index except for the last one.

Example 2

Input
[22, 21, 22, 23]
Output
[3, 1, 1, 0]
Explanation:

At index 0 (22), the next greater element is 23 which is 3 indexes away.

At index 1 (21), the next greater element is 22 which is 1 index away.

At index 2 (22), the next greater element is 22 which is 1 index away.

At index 3, there are no greater element.

Loading...
Input
[20, 21, 22, 23]
Output
[1, 1, 1, 0]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!