본문 바로가기

Develop/코딩인터뷰

[코딩인터뷰] 백준 2661번

반응형

백준 2661번
백준 2661번

def is_possible(l):
    for d in range(1, len(l) // 2 + 1):
        for s in range(len(l) - (d*2) + 1):
            a_start = s
            a_end = s + d
            a = l[a_start:a_end]

            b_start = a_end
            b_end = a_end + d
            if b_end > len(l):
                break

            b = l[b_start:b_end]

            if a == b:
                return  False
    return True

n = int(input())
l = []
value = (1, 2, 3)
pos = [[True, True, True] for _ in range(n)]
while len(l) < n:
    i = len(l)
    is_appended = False
    for v in value:
        if not pos[i][v - 1]:
            continue
        l.append(v)
        pos[i][v - 1] = False
        if is_possible(l):
            is_appended = True
            break
        else:
            l.pop()

    if not is_appended:
        for k in range(3):
            pos[i][k] = True
        l.pop()

answer = ''
for k in l:
    answer += str(k)
print(answer)
반응형