The problem comes off as pretty difficult at the start but the idea is very straight-forward, we just generate a lookup table for all difficulties.
We do this because the abilities of the workers can be anything from 0 to max(difficulty)
where difficulty is the array given to us containing all the difficulty to profit mappings.
Since it’s told in the description that if any ability of a worker does not have a corresponding difficulty in the input, then to consider the smallest closest difficulty.
For eg.
Difficulty | Profit |
---|---|
3 | 20 |
5 | 35 |
8 | 45 |
Then a worker with ability of 3 will bring a profit of 20, and a worker with ability of 4 will also bring a profit of 20.
A worker with ability of 2 will bring 0 profit.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where
maxDiff = max(difficulty)
-
Memory The memory usage is since we use the
profitLookup
array to store cumulative profits for all difficulties up until maxDiff.
— A