Блог пользователя Darth

Автор Darth, история, 6 лет назад, По-английски

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!

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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
»
6 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится
O(n) solution