Sharing answer codes of mine about HackerRank: Lily’s Homework.

HackerRank: Lily’s Homework (in Algorithm)

Problem Statement

Whenever George asks Lily to hang out, she’s busy doing homework. George wants to help her finish it faster, but he’s in over his head! Can you help George understand Lily’s homework so she can hang out with him?

Consider an array of distinct integers, . George can swap any two elements of the array any number of times. An array is beautiful if the sum of among is minimal possible (after, possibly, performing some swaps).

Given the array A, find and print the minimum number of swaps that should be performed in order to make the array beautiful.

Answer Code (in Python3)

  • Time complexity: ( is the number of elements in the list)
  • Space complexity :
#!/bin/python3
import sys

def lilysHomework(arr):
    org = list(arr)
    arr.sort()

    dic = {}
    for i in range(len(arr)):
        dic[org[i]]=i

    swp = 0
    for i in range(len(arr)):
        if arr[i] != org[i]:
            swp += 1
            #index = org.index(arr[i])
            index = dic[arr[i]]
            dic[org[i]] = index
            org[i], org[index] = org[index], org[i]

    return swp

if __name__ == "__main__":
    n = int(input().strip())
    arr = list(map(int, input().strip().split(' ')))
    rev = list(arr)
    rev.reverse()
    asc = lilysHomework(arr)
    desc = lilysHomework(rev)
    print (min(asc,desc))