728x90
🔗 Problem Link
❔Thinking
- 주어진 과제를 조건에 맞춰 해결할때, 해결한 과제 순으로 반환한다.
- 새로운 과제는 잠시 멈춘 과제보다 언제나 우선적으로 해결한다.
- 잠시 멈춘 과제는 최근에 멈춘 순서대로 진행한다.
💻Solution
def solution(plans):
completed = []
stopped = []
plans = sorted(plans, key=lambda x: x[1])
now_name, now_start, now_playtime = plans[0][0], int(plans[0][1].split(":")[0]) * 60 + int(plans[0][1].split(":")[1]), int(plans[0][2])
for idx in range(1, len(plans)):
new_name, new_start, new_playtime = plans[idx]
new_start = int(new_start.split(":")[0]) * 60 + int(new_start.split(":")[1])
new_playtime = int(new_playtime)
# 이전 과제가 끝나고, 곧바로 새로운 과제가 시작하는 경우
if new_start == now_start+now_playtime:
completed.append(now_name)
now_name, now_start, now_playtime = new_name, new_start, new_playtime
elif new_start > now_start + now_playtime: # 새로운 과제 시작까지 시간이 남는 경우
completed.append(now_name)
left_time = new_start - (now_start+now_playtime)
while stopped and left_time>0:
stop_name, stop_lefttime = stopped.pop()
if stop_lefttime - left_time > 0:
stop_lefttime -= left_time
left_time = 0
stopped.append([stop_name, stop_lefttime])
else:
left_time -= stop_lefttime
completed.append(stop_name)
now_name, now_start, now_playtime = new_name, new_start, new_playtime
else: # 진행중인 과제를 멈춰야 하는 경우
now_playtime -= new_start - now_start
stopped.append([now_name, now_playtime])
now_name, now_start, now_playtime = new_name, new_start, new_playtime
# 현재 진행중인 과제 완료
completed.append(now_name)
# 남은 중단된 과제 모두 완료
while stopped:
completed.append(stopped.pop()[0])
return completed
🗝️keypoint
- 과제 해결의 상황을 아래 세가지로 구분지어 구현할 수 있다.
- 이전 과제 종료와 동시에 새로운 과제 시작
- 이전 과제 종료와 새로운 과제 사이에 잠시 멈춰둔 과제 해결
- 이전 과제를 멈추고 새로운 과제 시작
- plans의 모든 과제를 해결하면, 진행중인 과제는 멈춤 없이 해결한다.
- 24시로 표시된 시간을 분으로 계산하여 비교하면 보다 편리하게 계산할 수 있다.
'코딩테스트 > Python' 카테고리의 다른 글
[Programmers] Level 2. 요격 시스템 (0) | 2025.01.18 |
---|---|
[Baekjoon] 1101. Fly me to the Alpha Centauri (0) | 2025.01.17 |
[Beakjoon] 9663. N-Queen (0) | 2025.01.16 |
[Programmers] Level 2. 광물 캐기 (0) | 2025.01.11 |
[Beakjoon] 4195. 친구 네트워크 (0) | 2025.01.08 |