[Python] BOJ 12825번. Next Permutation

12825번. Next Permutation

문제 링크

풀이 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 12825번. Next Permutation


import sys
input = sys.stdin.readline

n = int(input())
nList = list(map(int, input().split()))

i = n-1
while i > 0 and nList[i-1] >= nList[i]:
    i -= 1
j = n-1
while nList[i-1] >= nList[j]:
    j -= 1
nList[i-1], nList[j] = nList[j], nList[i-1]

tList = [nList[k] for k in range(i)]
for k in range(j, n):
    tList.append(nList[k])
if abs(i-1-k) > 1:
    for k in range(j-1, i-1, -1):
        tList.append(nList[k])
print(*tList)

비고