I study algorithms, to learn, I do exercises that appear on sites like leetetcode and hacker rank. There is an exercise that I don't know where to start:
New disease spreads throughout the day
i = 0, so that in
i-ésimoa day ago
inew cases. Fortunately, there is treatment and a hospital can provide treatment. However, the hospital has a
capacidade máxima de camas Cwhich are available initially on the day
i = 0. The hospital must have a bed available to admit a new patient. The hospital can then treat up to
kpatients per day and treatment
é efetivo após um dia, which means that the hospital can heal up to
kpatients during the day
iand therefore distribute them and make your bed available again the day
i + 1. Write an algorithm to find out when the hospital will no longer be able to accept all new patients. It is the first day that one or more patients will not be admitted. Write your solution in a Python function
Hospital_Overflow(C, k). The time complexity of your solution should be
For example, hospital_overflow (5.2) should return 5. Indeed, on day 1, there is 1 patient in the hospital; this patient is cured and released on day 2, when two other patients are admitted; these two are cured on day 3, when three patients are admitted; on day 4, only 2 of the 3 hospital patients are cured and 4 new patients are admitted, filling the 5 beds. Therefore, on the 5th, the hospital will not be able to admit all new cases.
The method I would do would be to create a bunch and add new patients to the queue while deleting those who have been cured, but this has an O (n) complexity. I read that queues with this monotonous property of integers can be resolved in O (log (n)), but I couldn't figure out how, I'm looking for it help to understand how to solve the problem in the required complexity.