최대 1 분 소요

사용 언어: Python3

문제

스크린샷 2023-04-24 오후 3 04 02 스크린샷 2023-04-24 오후 3 04 16

풀이

내 풀이

def process(lst):
    mx, mn = max(lst), min(lst)
    lst[lst.index(mx)] -= 1
    lst[lst.index(mn)] += 1

L = int(input())
lst = list(map(int, input().split()))
M = int(input())

for _ in range(M):
    process(lst)

print(max(lst) - min(lst))

다른 풀이 - sort() 사용

def process(lst):
    lst.sort()
    lst[-1] -= 1
    lst[0] += 1

L = int(input())
lst = [int(x) for x in input().split()]
M = int(input())

for _ in range(M):
    process(lst)

lst.sort() # 맨 마지막 process() 수행 후 정렬
print(lst[-1]-lst[0])
  • M번 sort 하는 방식
    • sort 한 뒤, 맨 처음 원소 - 1 하고 맨 마지막 원소 + 1 한다.


💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기