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// C++ solution using sorting and linear scan
2#include <iostream>
3#include <vector>
4#include <unordered_map>
5#include <algorithm>
#include <iomanip>
struct Activity {
int machine_id;
int process_id;
std::string activity_type;
double timestamp;
};
int main() {
std::vector<Activity> activities = {
{0, 0, "start", 0.712},
{0, 0, "end", 1.520},
{0, 1, "start", 3.140},
{0, 1, "end", 4.120},
{1, 0, "start", 0.550},
{1, 0, "end", 1.550},
{1, 1, "start", 0.430},
{1, 1, "end", 1.420},
{2, 0, "start", 4.100},
{2, 0, "end", 4.512},
{2, 1, "start", 2.500},
{2, 1, "end", 5.000}
};
std::unordered_map<int, double> totalTimes;
std::unordered_map<int, int> processCounts;
// Sort activities based on machine_id and process_id
std::sort(activities.begin(), activities.end(), [](const Activity &a, const Activity &b) {
if (a.machine_id != b.machine_id) return a.machine_id < b.machine_id;
return a.process_id < b.process_id;
});
for (size_t i = 0; i < activities.size(); i += 2) {
const Activity &start = activities[i];
const Activity &end = activities[i + 1];
double processTime = end.timestamp - start.timestamp;
totalTimes[start.machine_id] += processTime;
processCounts[start.machine_id]++;
}
// Output average processing time per machine
std::cout << "+------------+-----------------+" << std::endl;
std::cout << "| machine_id | processing_time |" << std::endl;
std::cout << "+------------+-----------------+" << std::endl;
for (const auto &entry : totalTimes) {
int machine_id = entry.first;
double averageTime = entry.second / processCounts[machine_id];
std::cout << "| " << machine_id << " | " << std::fixed << std::setprecision(3) << averageTime << " |" << std::endl;
}
std::cout << "+------------+-----------------+" << std::endl;
return 0;
}
The C++ implementation follows the same logic as the Java solution: sorting the activities list, performing a dual-index linear scan, and calculating the average process time for each machine. Output is formatted using iomanip for precision.