Any one to help with a solution for this task here?

by Ronnie Web   Last Updated April 15, 2019 17:05 PM

John is a famous shopkeeper who sells n items in her shop. She assigns each item a unique popularity rating in the inclusive range from 1 to n.

The shop only has one shelf, so the items are displayed array-style in a single row spanning from left to right in a random order. He wants to rearrange the items on the shelf by decreasing popularity rating such that the rating for the i item is always greater than the popularity rating of the (i + 1) item. Mary can swap any two items, i and j, in a single operation.

Specification minimumSwaps(ratings)

Parameters

ratings: Array - an array of numbers indicating the popularity rating of each item on the shelf.

Return Value

Number - denotes the minimum number of swap operations Mary must perform to order all n items by decreasing popularity rating.

Constraints

n = number of items in ratings array

each rating value ( arri ) will be unique

1 ≤ n ≤ 2 × 105

1 ≤ arri ≤ n

Tags : javascript


Related Questions


Updated September 22, 2017 04:05 AM

Updated March 27, 2015 13:01 PM

Updated April 04, 2015 15:02 PM

Updated April 07, 2015 18:02 PM

Updated April 09, 2015 19:02 PM