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// JavaScript solution using a map to track start and end times
2const activityData = [
3 {machine_id: 0, process_id: 0, activity_type: 'start', timestamp: 0.712},
4 {machine_id: 0, process_id: 0, activity_type: 'end', timestamp: 1.520},
5 {machine_id: 0, process_id: 1, activity_type: 'start', timestamp: 3.140},
6 {machine_id: 0, process_id: 1, activity_type: 'end', timestamp: 4.120},
7 {machine_id: 1, process_id: 0, activity_type: 'start', timestamp: 0.550},
8 {machine_id: 1, process_id: 0, activity_type: 'end', timestamp: 1.550},
9 {machine_id: 1, process_id: 1, activity_type: 'start', timestamp: 0.430},
10 {machine_id: 1, process_id: 1, activity_type: 'end', timestamp: 1.420},
11 {machine_id: 2, process_id: 0, activity_type: 'start', timestamp: 4.100},
12 {machine_id: 2, process_id: 0, activity_type: 'end', timestamp: 4.512},
13 {machine_id: 2, process_id: 1, activity_type: 'start', timestamp: 2.500},
14 {machine_id: 2, process_id: 1, activity_type: 'end', timestamp: 5.000}
15];
16
17const machineTimes = new Map();
18
19for (const {machine_id, process_id, activity_type, timestamp} of activityData) {
20 const key = `${machine_id},${process_id}`;
21 if (!machineTimes.has(key)) machineTimes.set(key, []);
22 machineTimes.get(key).push(timestamp);
23}
24
25const avgTimes = new Map();
26const processCount = new Map();
27
28for (const [key, times] of machineTimes.entries()) {
29 const [machine_id] = key.split(',').map(Number);
30 const processTime = Math.abs(times[1] - times[0]);
31 if (!avgTimes.has(machine_id)) avgTimes.set(machine_id, 0);
32 if (!processCount.has(machine_id)) processCount.set(machine_id, 0);
33 avgTimes.set(machine_id, avgTimes.get(machine_id) + processTime);
34 processCount.set(machine_id, processCount.get(machine_id) + 1);
35}
36
37const result = [];
38for (const [machine_id, totalTime] of avgTimes) {
39 const averageTime = parseFloat((totalTime / processCount.get(machine_id)).toFixed(3));
40 result.push({ machine_id, processing_time: averageTime });
41}
42
43console.table(result);
44
In this JavaScript solution, a map data structure is employed to keep track of the start and end times for each process on every machine. The total process times are added, and the average is computed for each 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.