Each cycle, one of the three events happens:
- Option 1: If there are at least
conversionRatePresources, then the lastconversionRatePresources are removed and oneAis added at the beginning of the array. - Option 2: If there is at least one
A, the lastAchanges toP. - Option 3: If neither Option 1 nor Option 2 can be completed, then the process halts.
Follow the process and compute how many cycles will pass until the process halts.
Refer to the examples below for better understanding.
Note: You are expected to provide a solution with time complexity not worse than O(resources.length * conversionRate^2).
Example 1:
For resources = ["A", "A", "A", "P", "P", "P"] and conversionRate = 2, the output should be solution(resources, conversionRate) = 13.
This Uber OA problem is a deterministic simulation with two prioritized state transitions: convert conversionRate trailing P resources into one leading A when possible; otherwise flip the last A into P; otherwise stop. The efficient solution avoids rebuilding the array and instead tracks only the counts of A and P, updating them per cycle until no rule applies. This yields a clean linear-time simulation over the number of cycles while staying within the required complexity bound.