Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 621 · Greedy + Heap / Scheduling

Task Scheduler

LeetCode 621 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Max-heap of counts + cooldown queue.

ಮಧ್ಯಮArrayHash TableGreedySortingHeap (Priority Queue)
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Task Scheduler (LeetCode 621) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Greedy + Heap / Scheduling ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Simulate the CPU clock like an event loop: always run the task with the most remaining copies (max-heap). After running it, it cools down in a queue with the time it becomes available again; when that time comes, it goes back into the heap.

Approach

  1. 1

    Max-heap of remaining counts (stored negative).

  2. 2

    Each tick: time++, run the top task if any; if copies remain, queue it until time + n.

  3. 3

    If the queue's front is ready, push it back into the heap.

  4. 4

    Stop when both are empty.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Heap + cooldown queue simulationPythonO(T log 26) time
import heapq
class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        #same like event loop
        #will keep a pq to maintaint the high freq items
        #will keep a q to store the future items
        freq_map = Counter(tasks)
        max_heap = [-freq for freq in freq_map.values()]
        heapq.heapify(max_heap)
        q = deque()
        #execution will happen till the pq not empty or q is not empty
        time =0
        while max_heap or q:
            time+=1
            if max_heap:
                high_freq_process = heapq.heappop(max_heap)
                #if still has some freq
                if high_freq_process+1<0:
                    #when in the feature will be it's next eligbile 
                    q.append((high_freq_process+1,time+n))
                #check in the q if something is eligilble
            if q:
                if q[0][1]==time:
                    eligible_item = q.popleft()
                    #put back into 
                    heapq.heappush(max_heap,eligible_item[0])
        return time


            
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Task Scheduler — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Task Scheduler ಹಿಂದಿನ intuition ಏನು?

Simulate the CPU clock like an event loop: always run the task with the most remaining copies (max-heap). After running it, it cools down in a queue with the time it becomes available again; when that time comes, it goes back into the heap.

Task Scheduler (LeetCode 621) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Max-heap of remaining counts (stored negative). 2. Each tick: time++, run the top task if any; if copies remain, queue it until time + n. 3. If the queue's front is ready, push it back into the heap. 4. Stop when both are empty.

Task Scheduler ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(T · log 26). Space: O(26). (Accepted approach: Heap + cooldown queue simulation.)

Task Scheduler ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

The formula (maxCount − 1)·(n + 1) + #maxCount gives the answer in O(T) without simulating.