AMAZON Coding Question β Solved
The engineering team at an Amazon fulfillment center is optimizing n high-performance printers, where each printer can print pages[i] number of pages. Each printer can be in exactly one of three states: operational, idle, or suspended.
Printers initially start in an idle state and can be activated one by one. However, if too many printers are active at once, some will get suspended due to their threshold limit defined by the suspension rule below.
Suspension Rule: If there are at least x operational printers, all such printers with threshold[i] β€ x will get suspended and stop printing.
Task: The goal is to determine the maximum number of pages that can be printed before printers get suspended.
Note: Activating a printer with threshold[i] = x allows it to print pages[i] pages. However, once at least x printers are active, their pages get printed first, and then all the printers with threshold β€ x will get suspended immediately. Choosing the activation order carefully is therefore crucial to maximize the total printed pages before suspensions occur.
Example:
n = 4
pages = [2, 6, 10, 13]
threshold = [2, 1, 1, 1]
The optimal way to maximize the number of pages printed is as follows (using 1-based indexing):
First, the engineers decide to activate the 4th printer, which prints 13 pages. At this point, the total number of operational printers is 1. The printing of 13 pages is completed first, followed by the suspension of any printers exceeding their threshold.
Next, since the threshold for printers 2, 3, and 4 is 1, and there is now 1 operational printer (4th printer), these printers become damaged. Thus, all the printers (2nd, 3rd, and 4th) with threshold = 1 get suspended and stop functioning.
Next, the only printer the team can turn on is printer 1. By activating printer 1, they print 2 more pages. The number of operational printers is now 1, and because threshold[1] = 2, printer 1 will not be suspended and remains functional.
Thus, the total number of pages printed is 13 (from printer 4) + 2 (from printer 1) = 15. Hence, the answer is 15.
Function Description
Complete the function getPages in the editor below.
getPages has the following parameter(s):
int pages[n]: number of pages that printers can print
int threshold[n]: threshold values of printers
Asked in:
AMAZON