[Baekjoon] 1138 한 줄로 서기 python

sorzzzzy·2021년 8월 4일
0

Baekjoon Algorithm

목록 보기
11/46
post-thumbnail

🏷 문제


💡 코드

# 키가 i 일 때
# 1. 왼쪽에 나보다 큰사람이 0인 경우 -> 리스트의 왼쪽부터 봤을때 가장 처음 0의 자리에 i를 넣는다.
# 2. 왼쪽에 나보다 큰사람이 j명인 경우 -> 리스트의 왼쪽부터 0 을 j만큼 지나친 이후의 0의 자리에 i를 넣는다.
n = int(input())
lines = list(map(int, input().split()))
res = [0] * n
cnt = 0

for i in range(n):
    # 왼쪽에 나보다 큰 사람이 없는 경우
    if lines[i] == 0:
        for j in range(n):
            if res[j] == 0:
                res[j] = i+1
                break
    # 왼쪽에 나보다 큰 사람이 있는 경우
    else:
        # 큰 사람이 cnt만큼 있음
        cnt = lines[i]
        for j in range(n):
            if res[j] == 0 and cnt != 0:
                cnt -= 1
            elif res[j] == 0 and cnt == 0:
                res[j] = i+1
                break
            else:
                continue
    # print(*res)
print(*res)

🔑

profile
Backend Developer

0개의 댓글