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

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

https://uva.onlinejudge.org/external/110/11078.pdf

Please go through the link..

How can i solve this problem with linear scan. O(n^2) solution is very obvious to me. But linear scan solution o(n), i have no idea,Please help?

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

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

While scanning from left to right in the array, just keep a variable 'maxi' storing the maximum value you have encountered so far and at the current step you are sure that whatever 'maxi' is storing is the maximum value amongst all the senior of current candidate and just calculate this difference and find the maximum among all these differences.

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

please can someone, help me understand the above illustrated case?

  • »
    »
    7 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

    When i = 1, maxi = 0, curnum = 4, now maxi becomes 4, When i = 2, maxi = 4, curnum = 3, update ans to 4 — 3 = 1; When i = 3, maxi = 4, curnum = 2, update ans to 4 — 2 = 2; When i = 4, maxi = 4, curnum = 1, update ans to 4 — 1 = 3; [Assuming 1-indexing].

»
2 месяца назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I have a question. If the complexity is O(n^2) and given the constraints, the problem should be accepted. or I am wrong?