美文网首页HackerRank
HackerRank:Minimum Swaps 2 Pytho

HackerRank:Minimum Swaps 2 Pytho

作者: 流浪山人 | 来源:发表于2019-11-18 22:12 被阅读0次

    题目

    You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order.

    For example, given the array we perform the following steps:

    i arr swap (indices)
    0 [7, 1, 3, 2, 4, 5, 6] swap (0,3)
    1 [2, 1, 3, 7, 4, 5, 6] swap (0,1)
    2 [1, 2, 3, 7, 4, 5, 6] swap (3,4)
    3 [1, 2, 3, 4, 7, 5, 6] swap (4,5)
    4 [1, 2, 3, 4, 5, 7, 6] swap (5,6)
    5 [1, 2, 3, 4, 5, 6, 7]
    It took swaps to sort the array.

    Function Description

    Complete the function minimumSwaps in the editor below. It must return an integer representing the minimum number of swaps to sort the array.

    minimumSwaps has the following parameter(s):

    arr: an unordered array of integers
    Input Format

    The first line contains an integer, , the size of .
    The second line contains space-separated integers .

    Constraints

    Output Format

    Return the minimum number of swaps to sort the given array.

    Sample Input 0

    4
    4 3 1 2
    Sample Output 0

    3
    Explanation 0

    Given array
    After swapping we get
    After swapping we get
    After swapping we get
    So, we need a minimum of swaps to sort the array in ascending order.

    Sample Input 1

    5
    2 3 4 1 5
    Sample Output 1

    3
    Explanation 1

    Given array
    After swapping we get
    After swapping we get
    After swapping we get
    So, we need a minimum of swaps to sort the array in ascending order.

    Sample Input 2

    7
    1 3 5 2 4 6 7
    Sample Output 2

    3
    Explanation 2

    Given array
    After swapping we get
    After swapping we get
    After swapping we get
    So, we need a minimum of swaps to sort the array in ascending order.

    题目解析

    将数组先排序,以<key,value>的形式保存到Map中,遍历未排序的数组,将元素逐个和已排序数组对应位置元素比较,若相等则跳过,不等则从Map中取出已排序元素的index更新未排序元素的value上(交换操作),同时swap list中的元素.这样遍历完成之后,就得到整个交换过程

    Answer

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    # Complete the minimumSwaps function below.
    def minimumSwaps(arr):
        ref_arr = sorted(arr)
        index_dict = {v: i for i,v in enumerate(arr)}
        swaps = 0
        print(index_dict)
        
        for i,v in enumerate(arr):
            correct_value = ref_arr[i]
            if v != correct_value:
                to_swap_ix = index_dict[correct_value]
                print(to_swap_ix)
                arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
                index_dict[v] = to_swap_ix
                index_dict[correct_value] = i
                swaps += 1
                
        return swaps 
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input())
    
        arr = list(map(int, input().rstrip().split()))
    
        res = minimumSwaps(arr)
    
        fptr.write(str(res) + '\n')
    
        fptr.close()
    
    

    相关文章

      网友评论

        本文标题:HackerRank:Minimum Swaps 2 Pytho

        本文链接:https://www.haomeiwen.com/subject/zkriictx.html