List Reduction Algorithm

Forgive me, but I am very confused, and I can not find sources pointing to me in the right direction.

The specified list of n elements:

[3, 6, 5, 1]

Decrease values ​​that do not exceed the size of the list, keeping the priority values ​​relative to each other (in the original order).

Limitations:

  • Order must be saved
  • Elements> = 0
  • Distinctive values

I try to stay away from sorting and creating a new list, but changing the list in place.

What should be my expected result:

[1, 3, 2, 0]

Is there an algorithm that exists for this problem?

+1
source share
2 answers

You can do it in O (n ^ 2).

n , (while >= i) i , i 0 n-1

, - , , .

:

Input: 3  6  5  1

3  6  5  0*
1* 6  5  0
1  6  2* 0
1  3* 2  0

: , >= 0

+1

, , , .

, 4, .

if, , ; , , ( 4).

. , , . , !

0

All Articles