Grokking the Coding Interview: Patterns for Coding Questions
Vote
0% completed
Problem Challenge 2: Scheduling Tasks (hard)
Problem Statement
You are given a list of tasks that need to be run, in any order, on a server. Each task will take one CPU interval to execute but once a task has finished, it has a cooling period during which it can’t be run again. If the cooling period for all tasks is ‘K’ intervals, find the minimum number of CPU intervals that the server needs to finish all tasks.
If at any time the server can’t execute any task then it must stay idle.
Example 1:
Input: [a, a, a, b, c, c], K=2
Output: 7
Explanation: a -> c -> b -> a -> c -> idle -> a
Example 2:
.....
.....
.....
Like the course? Get enrolled and start learning!
Shashwat Kumar
· 2 years ago
strconsists of only lowercase English letters. - This is not correct as there are uppercase letters also.
P
Pete Stenger
· 2 years ago
from heapq import * from collections import deque, Counter class Solution: def reorganizeString(self, str, k): counts = Counter(str) heap = [ (-value, char) for char, value in counts.items() ] heapify(heap) if -heap[0][0] > (len(str) + k - 1)//k: return '' res = list('_' * len(str)) i = 0 offset = 0 while heap: count, char = heappop(heap) while count < 0: res[i] = char i += k if i >= len(res): offset += 1 i = offset count += 1 return ''.join(res)