Created
September 29, 2018 00:06
-
-
Save roxsula/3577d97fc5a015b1cc5cab0e48db523e to your computer and use it in GitHub Desktop.
Minimum Swaps 2 - Hackerrank - https://www.hackerrank.com/challenges/minimum-swaps-2/problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the minimumSwaps function below. | |
def minimumSwaps(arr): | |
swaps = 0 | |
i = 0 | |
while i < len(arr): | |
if arr[i] != i+1: | |
idx_fixed = arr[i] - 1 | |
arr[i],arr[idx_fixed] = arr[idx_fixed],arr[i] | |
i -= 1 | |
swaps += 1 | |
i += 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment