by Ronnie Web
Last Updated April 15, 2019 11:05 AM - source

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)

// Here is my solution to the problem above

```
function minimumSwaps(ratings) {
var swaps = 0;
for (var i = 0; i < ratings.length; i++) {
for (var j = i + 1; j < ratings.length; j++) {
if (ratings[i] == ratings[j]) {
ratings[i] = ratings[i] + ratings[j];
} else {
swaps = swaps + 1;
}
}
}
return swaps;
}
minimumSwaps([4, 3, 1, 2]);
```

- Serverfault Help
- Superuser Help
- Ubuntu Help
- Webapps Help
- Webmasters Help
- Programmers Help
- Dba Help
- Drupal Help
- Wordpress Help
- Magento Help
- Joomla Help
- Android Help
- Apple Help
- Game Help
- Gaming Help
- Blender Help
- Ux Help
- Cooking Help
- Photo Help
- Stats Help
- Math Help
- Diy Help
- Gis Help
- Tex Help
- Meta Help
- Electronics Help
- Stackoverflow Help
- Bitcoin Help
- Ethereum Help