← 返回 sofi 的题目列表Count Complete Highway Journeys from Sensor Logs
类型:online_judge
A highway has multiple sensor locations: entry, exit, and intermediate checkpoints. Each sensor logs when a car passes.
You are given time-ordered logs logs. Each log entry indicates that at a given time, a sensor saw a specific car.
Define a journey as: the same car completes an entry -> exit within the logs (with zero or more checkpoints in between). Each completed entry-to-exit counts as one journey.
Implement a function that counts how many journeys are present in logs.
Input (make assumptions explicit in your implementation)
logs: list of records sorted by time
each record includes at least: timestamp, sensorId, carId
a sensor metadata source that maps sensorId to entry / exit / checkpoint
Output
integer: total number of completed journeys
Typical constraints
logs are already sorted by timestamp
carId/sensorId are strings or integers
a car may appear in multiple journeys
Example tests (for self-check)
Single completed trip: entry(A) -> checkpoint -> exit(A) => 1
Incomplete: entry(A) -> checkpoint => 0
Interleaving cars: entry(A), entry(B), exit(A), exit(B) => 2
Multiple trips same car: entry(A), exit(A), entry(A), exit(A) => 2
Checkpoints outside a trip: checkpoint(A), entry(A), checkpoint(A), exit(A) => 1
Example
Input
logs=[(1,'E','A'),(2,'C1','A'),(3,'X','A')], sensorType={'E':'entry','X':'exit','C1':'checkpoint'}
Output
1