엔지니어 게시판
LeetCode 솔루션 분류

[12/29] 1834. Single-Threaded CPU

컨텐츠 정보

본문

You are given n​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the i​​​​​​th​​​​ task will be available to process at enqueueTimei and will take processingTimei to finish processing.

You have a single-threaded CPU that can process at most one task at a time and will act in the following way:

  • If the CPU is idle and there are no available tasks to process, the CPU remains idle.
  • If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
  • Once a task is started, the CPU will process the entire task without stopping.
  • The CPU can finish a task then start a new one instantly.

Return the order in which the CPU will process the tasks.

 

Example 1:

Input: tasks =
태그 ,

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
from collections import deque
import heapq
class Solution:
    def getOrder(self, tasks: List[List[int]]) -> List[int]:
        res = []

        tasks = deque( sorted([ (e, p, idx) for idx, (e, p) in enumerate(tasks) ]) )
        min_heap = []
        heapq.heapify(min_heap)

        t = tasks[0][0]
        num = 0
        while tasks or min_heap:
            while tasks and t >= tasks[0][0]:
                e, p, idx = tasks.popleft()
                heapq.heappush(min_heap, (p, idx))
            if min_heap:
                p, idx = heapq.heappop(min_heap)
                res.append(idx)
                num += 1
                t += p
            elif tasks:
                t = tasks[0][0]
        return res




전체 194 / 2 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


알림 0