Amazon Online Assessment (OA)  Find The Highest Profit
An ecommerce company imports a type of fitness band from China and sell them in the US for a higher price. The company source the product from multiple suppliers, each with their own inventory. The suppliers raise the price of their product when inventory decreases due to scarcity. More specifically, the profit that the ecommerce company makes on each product sold is equal to the number of products left from the supplier.
Given a list of integers representing the number of products each supplier has and an integer representing the number of products sold, find the maximum profit the company can make.
Examples
Example 1:
Input:
inventories = [6, 4]
order = 4
Output: 19
Explanation:
There are two suppliers, with inventory of 4 and 6 respectively. A total of 4 items are ordered. We can make maximum profit by
 selling 1 item from the first supplier for 6
 selling 1 item from the first supplier for 5
 selling 1 item from the first supplier for 4, which brings down the inventory of the first supplier to 3
 selling 1 item from the second supplier for 4
The maximum profit is 6 + 5 + 4 + 4 = 19
.
Example 2:
Input:
inventories = [10, 10]
order = 5
Output: 46
Explanation:
The maximum profit we can generate is by
 selling 1 item for a profit of 10 from the first supplier
 selling 1 item for a profit of 10 from the second supplier
 selling 1 item for a profit of 9 from the first supplier
 selling 1 item for a profit of 9 from the second supplier
 selling 1 item for a profit of 8 from the first or second supplier
The maximum profit is 10 + 10 + 9 + 9 + 8 = 46
.
Try it yourself
Solution
The higher the stock, the higher profit we get, so it's always better to buy from a supplier with the highest stock. After each purchase, the supplier's stock would drop by 1, and we repeat this process until we have fullfilled all orders.
Let's visualize with a table,
where content of each cell denotes the order we buy each product,
and 
indicates products that we don't need since we have enough already:
inventories = [3, 6, 6]
, order = 14
Stock  Supplier 1  Supplier 2  Supplier 3 

6  1  2  
5  3  4  
4  5  6  
3  7  8  9 
2  10  11  12 
1  13  14   
Initially, supplier 2 and 3 have the highest stock (6), so we purchase products from them until all 3 suppliers have 3 products left. At this point, we have 5 more orders, so we purchase 6 products (2 from each supplier) with profit 3 and 2, and finally 2 products with profit 1.
To always purchase from a supplier with highest stock, we need to remember the stock of each supplier. Repeatedly removing the largest value while inserting new values, sounds like a good fit for priority queues / heaps, and so we have a solution:
1  + 
 
1  2 
 
2  3  
3  4 
 
4   
 
5  + 
 
5   
 
6  + 
 
7  + 
 
8  + 
 
9  + 
 
10  + 
 
11  + 
 
12  + 
 
13  + 
 
14  +  
6  15 
 
7  16 
 
8  17 
 
9  18 
 
10  19 

Optimization
That's a good first approach, but there is still plenty of room for improvement.
Consider if supplies and order are large:
inventories = [50000]
, order = 50000
In this case, we are essentially removing 50000, inserting 49999, removing 49999, inserting 49998, and so on.
We are in fact removing and inserting items from/into the priority queue order
times.
Note that order
is not the size but the value of an input,
which means the runtime is actually exponential to the input size
(see pseudopolynomial)!
If we look more closely, we'll see that our profit in this case is 50000 + 49999 + ... + 1
,
the sum of an arithmetic sequence,
which has a closed form formula:
sum(a, a + 1, ..., b  2, b  1) = (a + b  1) * (b  a) / 2
In another direction, consider if there are many suppliers with the same number of products:
inventories = [10, 10, 10, 10, 10]
, order = 50
Since we always pick from the suppliers with highest stock, if there are multiple suppliers with the same (but all highest) number of products, we will only move on to a lower number if we bought from all of them. So rather than processing one at a time, we could group suppliers with the same number of products and process each group at a time.
Let's visualize with a profit table of the example in the first solution:
Stock  Supplier 1  Supplier 2  Supplier 3 

6  6  6  
5  5  5  
4  4  4  
3  3  3  3 
2  2  2  2 
1  1  1   
The first solution essentially calculates 6 + 6 + 5 + 5 + 4 + 4 + 3 + 3 + 3 + 2 + 2 + 2 + 1 + 1
,
while we instead want to compute 2 * (6 + 5 + 4) + 3 * (3 + 2) + 2 * (1)
.
To do this, we first group suppliers by stock (1 supplier has 3 products, and 2 suppliers have 6 products), then sort by stock descending:
stocks = [(stock=6, count=2), (stock=3, count=1)]
By looking at the first 2 pairs, we can find out how many products the 2 suppliers can provide us before other suppliers can compete:
supply = (6  3) * 2 = 6
Since we need 14 products, we will take all 6 of them, and the proft gained is:
profit += 2 * sum(4, ..., 6)
Now there are 3 suppliers with the highest stock (2 from before and 1 joining the competition). Following the same logic, they can offer:
supply = (3  0) * 3 = 9
However we only need 8 more, so we take
full = floor(8 / 3) = 2
products from each of the 3 suppliers (at stock 3 and 2), and an extra of
part = 8 mod 3 = 2
products (at stock 1).
So we gain a profit of:
profit += 3 * sum(2, ..., 3) + 2 * (1)
Thus we have the optimized solution:
1   
 
1  + 
 
2  2  
3  3  
4  + 
 
5  + 
 
6  + 
 
7  + 
 
8  +  
4  9 
 
5   
 
10  + 
 
6   
 
11  + 
 
12  + 
 
13  + 
 
14  + 
 
15  + 
 
16  + 
 
17  + 
 
18  + 
 
19  + 
 
20  + 
 
21  + 
 
22  + 
 
23  + 
 
24  + 
 
25  + 
 
26  + 
 
27  + 
 
28  + 
 
29  + 
 
30  + 
 
31  + 
 
32  + 
 
33  + 
 
34  + 
 
35  +  
7  36 
 
8  37 
 
9  38 
 
10  39 
 
11  40 
