1661. Average Time of Process per Machine LeetCode Solution
In this guide, you will get 1661. Average Time of Process per Machine LeetCode Solution with the best time and space complexity. The solution to Average Time of Process per Machine problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Average Time of Process per Machine solution in C++
- Average Time of Process per Machine solution in Java
- Average Time of Process per Machine solution in Python
- Additional Resources

Problem Statement of Average Time of Process per Machine
Table: Activity
+—————-+———+
| Column Name | Type |
+—————-+———+
| machine_id | int |
| process_id | int |
| activity_type | enum |
| timestamp | float |
+—————-+———+
The table shows the user activities for a factory website.
(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.
machine_id is the ID of a machine.
process_id is the ID of a process running on the machine with ID machine_id.
activity_type is an ENUM (category) of type (‘start’, ‘end’).
timestamp is a float representing the current time in seconds.
‘start’ means the machine starts the process at the given timestamp and ‘end’ means the machine ends the process at the given timestamp.
The ‘start’ timestamp will always be before the ‘end’ timestamp for every (machine_id, process_id) pair.
It is guaranteed that each (machine_id, process_id) pair has a ‘start’ and ‘end’ timestamp.
There is a factory website that has several machines each running the same number of processes. Write a solution to find the average time each machine takes to complete a process.
The time to complete a process is the ‘end’ timestamp minus the ‘start’ timestamp. The average time is calculated by the total time to complete every process on the machine divided by the number of processes that were run.
The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places.
Return the result table in any order.
The result format is in the following example.
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
1661. Average Time of Process per Machine LeetCode Solution in C++
SELECT
StartActivity.machine_id,
ROUND(
AVG(EndActivity.timestamp - StartActivity.timestamp),
) AS processing_time
FROM Activity AS StartActivity
INNER JOIN Activity AS EndActivity
USING (machine_id, process_id)
WHERE
StartActivity.activity_type = 'start'
AND EndActivity.activity_type = 'end'
GROUP BY 1;
/* code provided by PROGIEZ */
1661. Average Time of Process per Machine LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1661. Average Time of Process per Machine LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.