파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 12. 29. 23:45

https://leetcode.com/problems/single-threaded-cpu/

 

Single-Threaded CPU - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

heap과 sort를 활용해서 풀면 된다.
운영체제 이론에서 비슷한걸 본 것 같은 느낌..

class Solution:
    def getOrder(self, tasks: List[List[int]]) -> List[int]:
        res = []
        tasks = sorted([(t[0], t[1], i) for i, t in enumerate(tasks)])
        i = 0
        h = []
        time = tasks[0][0]

        while len(res) < len(tasks):
            while (i < len(tasks)) and (tasks[i][0] <= time):
                heapq.heappush(h, (tasks[i][1], tasks[i][2]))
                i += 1
            
            if h:
                t_diff, original_index = heapq.heappop(h)
                time += t_diff
                res.append(original_index)
            elif i < len(tasks):
                time = tasks[i][0]
        
        return res
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.12.31
Today's Challenge  (0) 2022.12.30
Today's Challenge  (0) 2022.12.28
Today's Challenge  (0) 2022.12.27
Today's Challenge  (0) 2022.12.26
Comments