Worker uptime total

Write codeMedium13 min

Your task

A cron supervisor logs "<ts> START" when a worker comes up and "<ts> STOP" when it goes down, with ts as integer Unix seconds. The log is chronological and well-formed: events strictly alternate beginning with START, so a STOP always closes the most recent START. Given the log and the current time now (now >= every logged ts), return the worker's total uptime in seconds. If the log ends with a START, the final run is still going and counts up to now. An empty log means zero uptime.

Examples

total_uptime(events: list[str], now: int) -> int
Example 1
Inputtotal_uptime(["100 START", "250 STOP", "300 START"], 400)
Output250