[Programmers] Level 2. 과제 진행하기

2025. 1. 16. 15:50·코딩테스트/Python
728x90
반응형

🔗 Problem Link

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


❔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

  1. 과제 해결의 상황을 아래 세가지로 구분지어 구현할 수 있다.
    1. 이전 과제 종료와 동시에 새로운 과제 시작
    2. 이전 과제 종료와 새로운 과제 사이에 잠시 멈춰둔 과제 해결
    3. 이전 과제를 멈추고 새로운 과제 시작
  2. plans의 모든 과제를 해결하면, 진행중인 과제는 멈춤 없이 해결한다.
  3. 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
'코딩테스트/Python' 카테고리의 다른 글
  • [Programmers] Level 2. 요격 시스템
  • [Baekjoon] 1101. Fly me to the Alpha Centauri
  • [Beakjoon] 9663. N-Queen
  • [Programmers] Level 2. 광물 캐기
swwho
swwho
일상을 데이터화하다
  • swwho
    하루한장
    swwho
  • 전체
    오늘
    어제
    • 분류 전체보기 (187) N
      • ML_DL (38) N
        • MUJAKJUNG (무작정 시리즈) (17) N
        • 딥러닝 공부하기 (21)
      • 데이터사이언스 (1)
        • EDA (1)
        • 데이터과학을 위한 통계 (0)
      • 데이터엔지니어링 (2)
      • 논문리뷰 (2)
        • Computer Vision (2)
      • Python 활용하기 (12)
      • 코딩테스트 (127)
        • Python (109)
        • MySQL (14)
      • Git (3)
      • MySQL 활용하기 (0)
      • 일상 이야기 (1)
  • 블로그 메뉴

    • 홈
    • 태그
  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
swwho
[Programmers] Level 2. 과제 진행하기
상단으로

티스토리툴바