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

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

Given an array of n elements. ‘n’ operations needs to be performed on the array. For each operation start_index, end_index,trim_value is given. One has trim the value starting from the start_index to end_index inclusive. If the value at that index is more than or equal to trim value then make it equal to trim_value else leave it as it is. After performing ‘n’ operations find the maximum value of the array.

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

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

Am I the only one finding the problem statement a bit ambiguous ?

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

I think it could be solved using Segment Tree and Lazy Propagation

»
10 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Are you sure you need O(N)? All I can make up is O(NlogN).

»
10 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I think we can use the fact that a[i] will only decrease or at least, never increase.

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

First sort all the updates in the increasing order of trim value using radix sort or counting sort.

Now, maintain 2 arrays: next[i] and prev[i] denoting the immediate next and previous untrimmed element from i.

Iterate over the updates in the increasing order of trim value and trim all elements in the required range , updating next[] and prev[] as you go.

If we consider the initial sorting of updates to be O(N) , total time complexity is O(N)

Edit: sorry, seems like it might not be possible to keep track of prev and next in amortized O(N)