Darth's blog

By Darth, history, 4 months ago, In English,

Hello guys.

I was trying to solve this problem by Just Contest 2.0, about to find the next greater element. For exemple:

Array = [1, 5, 4, 7, 9]

Ans = [5, 7, 7, 9, -1]

-The input is 105.

My solution:

Solution

My implementation:

Implementation

Someone can explain-me where I did wrong? Or my idea is not correct?

Thaks!

 
 
 
 
  • Vote: I like it  
  • +3
  • Vote: I do not like it  

»
4 months ago, # |
  Vote: I like it +8 Vote: I do not like it

Your solution fails on a case like this:

4

4 1 2 5

Since the answer for 1 is 2 and 1 is not greater than 4 and 2 is also not greater than 4, it'll say that the answer for 4 is -1 when it is actually 5. Here's a (probably correct) solution for how you could solve this problem:

Spoiler
»
4 months ago, # |
  Vote: I like it +5 Vote: I do not like it
O(n) solution