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