You can save the pricing policy for each specific day in the interval tree. A tree should store all hours of the day. Along with the hourly boundaries, you need to save some information:
- Boolean apartment: is the price estimated? (e.g. R7.00 for the entire period of the interval)
- Whole price: the cost of an hour in a period. If
flat set to true, this will be the cost of the entire period.
You get parking entries in the input. I assume that these are timestamps for the arrival and outgoing time for the car. The first two hours are free, so you increase your arrival time by two hours. From the resulting timestamps, you can build a sequence of triplets (day, start time, end time). It may be empty (if the car owner is parked in less than two hours), this case is trivial: costs R0.00
Now the algorithm processing the sequence should look like this:
cost <- 0 while (NOT is_empty(sequence)) { dayTriplet <- pop(sequence) down <- dayTriplet.StartTime up <- dayTriplet.EndTime pricingTree <- getPriceTree(dayTriplet.dayId) while (down < up) { node <- findNode(pricingTree, down) nbHours <- min(up, node.up) - down if (node.flat) { cost <- cost + node.price } else { cost <- cost + nbHours*node.price } down <- down + nbHours } }
The algorithm is a bit wrong, but I felt that the ideas behind it were worth the shot. For example, if the car is parked after 18:00 for one day until 4:00 the next day, this algorithm will analyze (d, 18, 24) and (d + 1, 0, 4), and thus, the owner should bill twice per apartment pace of the period, and not just once. A similar system should do the trick for this problem. I just did not integrate it to leave a relatively clear algorithm.
source share