[HackerRank][Python3] Largest Permutation
2018. 9. 8. 21:54 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/largest-permutation/problem
My Solution :
#!/usr/bin/env python3
def largest_permutation(k, arr):
indexes = [0]*(n+1)
for i, a in enumerate(arr):
indexes[a] = i
for i in range(n):
if k == 0:
break
if arr[i] != n-i:
j = indexes[n-i]
arr[i], arr[j] = n-i, arr[i]
indexes[arr[j]] = j
k -= 1
return ' '.join(map(str, arr))
n, k = map(int, input().split())
arr = list(map(int, input().rstrip().split()))
result = largest_permutation(k, arr)
print(result)
'프로그래밍 > HackerRank' 카테고리의 다른 글
[HackerRank][Python3] Sum vs XOR (0) | 2018.09.09 |
---|---|
[HackerRank][Python3] Maximizing XOR (0) | 2018.09.08 |
[HackerRank][Python3] Permuting Two Arrays (0) | 2018.09.08 |
[HackerRank][Python3] Jim and the Orders (0) | 2018.09.08 |
[HackerRank][Python3] Priyanka and Toys (0) | 2018.09.08 |
[HackerRank][Python3] Beautiful Pairs (0) | 2018.09.08 |
[HackerRank][Python3] Maximum Perimeter Triangle (0) | 2018.09.08 |
[HackerRank][Python3] Grid Challenge (0) | 2018.09.08 |
최근에 달린 댓글 최근에 달린 댓글