← 返回 amazon 的题目列表Top K Slowest Requests from Logs
类型:online_judge
Problem: Find the Top K Slowest Requests from Logs
You are given a log file in which each line represents one completed request:
<request_id> <start_time> <end_time>
request_id is a string without spaces.
start_time and end_time are integer timestamps, where end_time >= start_time.
The request duration is end_time - start_time.
Return the k requests with the largest durations. Order the returned requests by:
duration in descending order;
request_id in lexicographic ascending order when durations tie.
Print the selected request IDs and durations.
Input Format
n k
request_id_1 start_time_1 end_time_1
...
request_id_n start_time_n end_time_n
Output Format
Print at most k lines:
request_id duration
Example
Input:
5 3
reqA 10 35
reqB 0 10
reqC 8 38
reqD 20 45
reqE 5 15
Output:
reqC 30
reqA 25
reqD 25
Constraints
1 <= n <= 200,000
1 <= k <= n
Timestamps are in [0, 10^9]
Aim for O(k) auxiliary space, excluding input storage.
Example
Input
5 3
reqA 10 35
reqB 0 10
reqC 8 38
reqD 20 45
reqE 5 15
Output
reqC 30
reqA 25
reqD 25