목차

호텔 방 배정

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호64063
문제명호텔 방 배정
레벨Level 4
분류

Disjoint Set

시간복잡도O(α(n)*n)
인풋사이즈n<=200,000
사용한 언어Python
해결날짜2021/01/17

풀이

코드

"""Solution code for "Programmers 64063. 호텔 방 배정".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/64063
- Solution link: http://www.teferi.net/ps/problems/programmers/64063
"""

from teflib import disjointset


def solution(k, room_number):

    def best_available_room(requested_room):
        s = djset.find(requested_room)
        try:
            return room_by_set[s]
        except KeyError:
            return s

    answer = []
    djset = disjointset.DisjointSet()
    room_by_set = {}
    for requested_room in room_number:
        assigned_room = best_available_room(requested_room)
        answer.append(assigned_room)
        next_available_room = best_available_room(assigned_room + 1)
        merged_set = djset.union(requested_room, assigned_room + 1)
        room_by_set[merged_set] = next_available_room
    return answer