challenges-algorithms icon indicating copy to clipboard operation
challenges-algorithms copied to clipboard

Classificar por altura

Open claudiuri opened this issue 5 years ago • 1 comments

Algumas pessoas estão em uma fila no parque. Existem árvores entre eles que não podem ser movidas. Sua tarefa é reorganizar as pessoas por suas alturas em uma ordem não decrescente, sem mover as árvores. As pessoas podem ser muito altas!

Para a = [-1, 150, 190, 170, -1, -1, 160, 180], a saída deve ser sortByHeight (a) = [-1, 150, 160, 170, -1, -1, 180, 190].

claudiuri avatar Oct 26 '20 23:10 claudiuri

Hi @claudiuri I want contribute to this issue under Hacktoberfest 2025 Here is my solution

  • First collect all values which are not equal to -1

  • For example For a = [-1, 150, 190, 170, -1, -1, 160, 180]

  • list=[150,190,170,160,180]

  • Next sort the above list in non decreasing order

  • list=[150,160,170,180]

  • Refill into a, skipping -1:

  • a[0] is -1 → keep it.

  • a[1] is person → place 150 (first sorted).

  • a[2] → place 160.

  • a[3] → place 170.

  • a[4] is -1 → keep.

  • a[5] is -1 → keep.

  • a[6] → place 180.

  • a[7] → place 190.

  • Result: [-1, 150, 160, 170, -1, -1, 180, 190]

Syed0206 avatar Oct 01 '25 15:10 Syed0206