10973번 – 이전수열

1

문제

https://www.acmicpc.net/problem/10973

풀이

코드

def intJoin(arr):
  return " ".join([str(i) for i in arr])

def answer():
  global arr
  for i in range(len(arr)-1, 0, -1):
    if arr[i] > arr[i-1]: continue
    left = arr[:i]
    right = sorted(arr[i:], reverse=True)
    for j in range(len(right)):
      if left[-1] > right[j]:
        temp = left[-1]
        left[-1] = right[j]
        right[j] = temp
        break
    return intJoin(left+sorted(right, reverse=True))
  return -1
  
n = int(input())
arr = list(map(int, input().split()))
print(answer())

Add Comment