황소개발자
백준 10973 파이썬 python : 이전 순열 @@황소처럼 우직하게@@ 스까묵자! 본문
반응형
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
반응형
'백준 문제 풀이' 카테고리의 다른 글
백준 10819 파이썬 python : 차이를 최대로 @@황소처럼 우직하게@@ 볼륨 쵀돼료! (0) | 2020.02.28 |
---|---|
백준 10974 파이썬 python : 모든 순열 @@황소처럼 우직하게@@ permutations 구현할 때, 이 코드 참고하세요~ (0) | 2020.02.28 |
백준 10972 파이썬 python : 다음 순열 @@황소처럼 우직하게@@ 가자 이 개쉐이야~ (0) | 2020.02.28 |
백준 9095 파이썬 python : 1, 2, 3 더하기 @@황소처럼 우직하게@@ 숏코딩 (0) | 2020.02.28 |
백준 1476 파이썬 python : 날짜 계산 @@황소처럼 우직하게@@ 어렵게 생각하지 마세요 (0) | 2020.02.27 |
Comments