dimanche 1 septembre 2019

Sort if you can; You are given a permutation of N numbers from 1 to N in an array A. You want to sort the array using this operation

Sort if you can;

You are given a permutation of N numbers from 1 to N in an array 
 A. You want to sort the array using this operation

1. Send the front-most element X places back in the array;
 2. Add x to the total cost.
   Sort if you can;

i find it difficult  to do. please  help asap

N<= 10^5 Thput 2: A [1, 3, 2]

Output 2: 3

Explanation 2:

=> Send 1 to index 1. (Cost +1)

A [3, 1, 2]

Send 3 to index 2. (Cost +2)

A = [1, 2, 3]

and the array is sorted.

Aucun commentaire:

Enregistrer un commentaire