백준 문제 풀이
백준 10973 파이썬 python : 이전 순열 @@황소처럼 우직하게@@ 스까묵자!
hjp845
2020. 2. 28. 05:49
반응형
import sys
input = sys.stdin.readline
n = int(input())
lst = list(map(int, input().split()))
def prev_permutation(lst):
if len(lst) == 1:
return [-1]
for i in range(len(lst) - 1, 0, -1):
if lst[i] <= lst[i - 1]:
break
if i == 1 and lst[0] < lst[1]:
return [-1]
# i - 1 인덱스에 위치한애가 바뀔 것이여
for j in range(len(lst) - 1, 0 , -1):
if lst[j] <= lst[i - 1]:
break
# j 인덱스하고 바뀔 것이여
tmp = lst[j]
lst[j] = lst[i - 1]
lst[i - 1] = tmp
lst = lst[:i] + sorted(lst[i:], reverse=True)
return lst
print(' '.join(map(str, prev_permutation(lst))))
뭐 다음 순열에서 등호 방향만 바꿔주면 된다~
아래 글이 다음 순열 코드이다.
백준 10972 파이썬 python : 다음 순열 @@황소처럼 우직하게@@ 가자 이 개쉐이야~
import sys input = sys.stdin.readline n = int(input()) lst = list(map(int, input().split())) def next_permutation(lst): if len(lst) == 1: return [-1] for i in range(len(lst) - 1, 0, -1): if lst[i] >..
hjp845.tistory.com
반응형