[Softeer] 회의실 예약

Gaanii·2024년 10월 22일
0

Problem Solving

목록 보기
59/210
post-thumbnail

문제링크


회의실 예약



풀이과정


이렇게 까지 불편하게 풀어야하나 ? 라고 생각했지만 굳이굳이 딕셔너리로 쓰겠다고 ....

일단 회의실 명을 key로 생각하고 회의실 예약 시간을 value로 생각했다.
그래서 다 딕셔너리에 저장을 한 후에 value를 시작시간을 기준으로 정렬했다.

그리고 나서 조건을 걸어야하는데,
1. 회의실 예약이 없으면 9시부터 18시까지 이용가능하다.
2. 회의실 예약이 1개면 해당 회의시간의 앞/뒤로 이용가능하다.
3. 회의실 예약이 여러개라면

  • 첫 회의 시간이 9시보다 이후라면 9시부터 첫 회의의 시작 시간 전까지 이용가능하다.
  • A번 회의의 끝나는 시간과 B번 회의의 시작 시간이 같지 않다면 그 사이에 이용 가능하다.
  • 마지막 회의 시간이 18시 이전에 끝난다면 마지막 회의의 끝나는 시간부터 18시까지 이용가능하다.

코드


import sys

N, M = map(int, input().split())

rooms = {}
result = {}

for _ in range(N):
    name = sys.stdin.readline().rstrip()
    rooms[name] = []
    result[name] = []

rooms = dict(sorted(rooms.items()))
result = dict(sorted(result.items()))

for i in range(M):
    r, s, t = sys.stdin.readline().split()
    rooms[r].append([int(s), int(t)])
    rooms[r] = sorted(rooms[r], key=lambda x: x[0])

for room, times in rooms.items():
    if len(times) == 0:
            result[room].append([9, 18])
    elif len(times) == 1:
            if times[0][0] > 9:
                result[room].append([9, times[0][0]])
            if times[0][1] < 18:
                result[room].append([times[0][1], 18])
    else:
        for i in range(len(times)):
            if i == 0:
                if times[i][0] > 9:
                    result[room].append([9, times[i][0]])
                else:
                    continue
            else:
                if times[i-1][1] != times[i][0]:
                    result[room].append([times[i-1][1], times[i][0]])
                
                if i == (len(times)-1) and times[i][1] < 18:
                    result[room].append([times[i][1], 18])

# 출력 !
result_list = list(result.items())
for room, times in result_list:
    print('Room ' + room + ':')
    if len(times) == 0:
        print('Not available')
    else:
        print(str(len(times)) + ' available:')
        for time in times:
            print(f'{time[0]:02d}-{time[1]}')
    if room != result_list[-1][0]:
        print('-----')
    


결과


정답

0개의 댓글