Lily's Homework

  • + 35 comments

    I've done it :) Passing all test :) My approach is:

    Prep stage:

    1. create map with:
      • keys : values from input list,
      • values : indexes of values from input list,
    2. make a copy of sorted input list

    Algo stage:

    Iterate through input list, and compare current value (lets call it cv) against value from sorted list (lets call it scv). If it is diffrent:

    • increment number of swaps
    • get index of the scv from map - map[scv],
    • in input list swap cv with scv,
    • update map[cv]=map[scv] (map[scv] does not need to be updated as we are not going to use it any more)

    Then you need to execute it on input list and reversed input list - the smaller return value - is the answer.

    Time complexity is equal to sort time complexity (usually O(n logn) ). Space O(n)