A browser-like resource loader runs at most limit fetches at once. Each request is [arrival, priority, duration]: it becomes available at time arrival, and once started it takes duration ms.
Whenever a slot is free and requests are waiting, the loader starts the waiting request with the highest priority; ties go to the earlier arrival, then to the smaller index. At any moment, finished fetches free their slots first, then newly arrived requests join the waiting list, then free slots are filled.
Write startTimes(requests, limit) that returns the time each request starts, in index order.
Input: requests = [[0,1,100],[10,1,50],[20,5,30]], limit = 1 Output: [0,130,100]
At 100 both waiting requests are available, and request 2 has the higher priority.
Input: requests = [[0,0,10],[0,0,10],[0,9,10]], limit = 2 Output: [0,10,0]
At time 0 two slots are free; request 2 (priority 9) and request 0 (earliest index) start.
0 <= requests.length <= 10^50 <= arrival <= 10^9, 0 <= priority <= 100, 1 <= duration <= 10^41 <= limit <= 100Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #751 Priority Fetch Queue. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.