Next Greater Element

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write nextGreater(nums): return a new array where position i holds the first number to the right of nums[i] that is greater than it, or -1 if there's none. Equal numbers don't count as greater. Don't change nums. For example, nextGreater([2, 1, 2, 4, 3]) is [4, 2, 4, -1, -1]. The checks include 100,000 numbers, so looking to the right from every position is too slow.

Your code must pass

  • Finds the next greater number for each position
  • Returns -1 where nothing to the right is greater
  • Doesn't count an equal number as greater
  • Is fast enough for 100,000 numbers