Sponsored
Sponsored
This approach involves using a data structure like a HashMap to store the start and end timestamps for each process on each machine. For each machine and process, compute the time taken using these timestamps. Then, calculate the average processing time for all machines.
Time Complexity: O(n), where n is the number of rows in the input. We iterate once through the list to collect timestamps.
Space Complexity: O(m), where m is the number of unique machine and process combinations.
1# Python solution using a dictionary to track start and end times
2from collections import defaultdict
3
4activity_data = [
5 (0, 0, 'start', 0.712),
6 (0, 0, 'end', 1.520),
7 (0, 1, 'start', 3.140),
8 (0, 1, 'end', 4.120),
9 (1, 0, 'start', 0.550),
10 (1, 0, 'end', 1.550),
11 (1, 1, 'start', 0.430),
12 (1, 1, 'end', 1.420),
13 (2, 0, 'start', 4.100),
14 (2, 0, 'end', 4.512),
15 (2, 1, 'start', 2.500),
16 (2, 1, 'end', 5.000)
17]
18
19machine_times = defaultdict(list)
20
21for machine_id, process_id, activity_type, timestamp in activity_data:
22 machine_times[(machine_id, process_id)].append(timestamp)
23
24avg_times = defaultdict(float)
25process_count = defaultdict(int)
26
27for (machine_id, process_id), times in machine_times.items():
28 if times[0] < times[1]:
29 process_time = times[1] - times[0]
30 else:
31 process_time = times[0] - times[1]
32 avg_times[machine_id] += process_time
33 process_count[machine_id] += 1
34
35result = []
36for machine_id in avg_times:
37 average_time = round(avg_times[machine_id] / process_count[machine_id], 3)
38 result.append((machine_id, average_time))
39
40# Output the result
41def print_result(result):
42 print("+------------+-----------------+")
43 print("| machine_id | processing_time |")
44 print("+------------+-----------------+")
45 for machine_id, avg_time in result:
46 print(f"| {machine_id} | {avg_time:.3f} |")
47 print("+------------+-----------------+")
48
49print_result(result)
50
This Python solution uses a dictionary to collect timestamps by process and machine. It then calculates process times and averages them by machine.
In this approach, we first sort the data by machine_id and process_id, ensuring that each start and end appears consecutively. We then do a single linear scan to compute the time taken for each process and maintain a cumulative sum for each machine, eventually computing the average processing time.
Time Complexity: O(n log n) due to sorting the list of activities.
Space Complexity: O(m), for storing accumulated processing times and counts by machine.
1// Java solution with sorting and linear scan
2import java.util.*
This Java implementation sorts the activity data and employs a dual-index scan to compute and accumulate the process time for each machine. The average times are then calculated and displayed.