You are given a system with m parallel indexers (workers), numbered from 0 to m-1. Documents arrive over time and are placed in a processing queue. Each document, when processed by an indexer, occupies that indexer exclusively for a given processing duration.
Document assignment works as follows (assume 0-based indexing for documents):
i
arrives at time
queue_time[i]
and requires
processing_time[i]
units of processing time.
base = i % m
. First, try to assign the document to indexer
base
.
j
is
available
for a document arriving at time
t
if its current processing (if any) completes no later than time
t
.
base
is
available
at time
queue_time[i]
, assign the document to indexer
base
.
m
:
(base + 1) % m
,
(base + 2) % m
, ...,
(base + m - 1) % m
. Assign the document to the
first
indexer found that is available at
queue_time[i]
.
queue_time[i]
, then this document is
dropped
and never processed.
i
is assigned to indexer
j
at time
queue_time[i]
, that indexer becomes busy until time
queue_time[i] + processing_time[i]
.
You are given:
m
— the number of indexers.
queue_time
of length
n
, where
queue_time[i]
is the time when document
i
is queued. The array is in strictly increasing order (i.e.,
queue_time[i] < queue_time[i+1]
).
processing_time
of length
n
, where
processing_time[i]
is the duration needed to process document
i
.
Assume n = len(queue_time) = len(processing_time).
Step 1 Design an algorithm that, using the above scheduling rules, computes:
Step 2
Extend your algorithm to also compute, for a given integer k (where 1 \le k \le m):
k
indexers that processed the largest number of documents. If there are ties, any order or any valid choice among tied indexers is acceptable.
k
indexers, as a value between 0% and 100%.
You should:
Example
Input:
m = 3
queue_time = [1, 2, 3, 7]
processing_time = [5, 4, 3, 2]
k = 2
for Step 2.
Processing simulation:
base = 0
. All indexers are free. Assign to indexer 0, which will be busy until time
1 + 5 = 6
.
base = 1
. Indexer 1 is free. Assign to indexer 1, busy until
2 + 4 = 6
.
base = 2
. Indexer 2 is free. Assign to indexer 2, busy until
3 + 3 = 6
.
base = 0
. Indexer 0 is free (available from time 6). Assign to indexer 0, busy until
7 + 2 = 9
.
All documents are successfully processed. Indexer 0 processed 2 documents; indexers 1 and 2 each processed 1 document.
Output:
4
0
[0, 1]
or
[0, 2]
75%
(3 out of 4 documents)