mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
99 lines
14 KiB
JSON
99 lines
14 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "1801",
|
||
"questionFrontendId": "1661",
|
||
"categoryTitle": "Database",
|
||
"boundTopicId": 491034,
|
||
"title": "Average Time of Process per Machine",
|
||
"titleSlug": "average-time-of-process-per-machine",
|
||
"content": "<p>Table: <code>Activity</code></p>\n\n<pre>\n+----------------+---------+\n| Column Name | Type |\n+----------------+---------+\n| machine_id | int |\n| process_id | int |\n| activity_type | enum |\n| timestamp | float |\n+----------------+---------+\nThe table shows the user activities for a factory website.\n(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.\nmachine_id is the ID of a machine.\nprocess_id is the ID of a process running on the machine with ID machine_id.\nactivity_type is an ENUM (category) of type ('start', 'end').\ntimestamp is a float representing the current time in seconds.\n'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.\nThe 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.</pre>\n\n<p> </p>\n\n<p>There is a factory website that has several machines each running the <strong>same number of processes</strong>. Write a solution to find the <strong>average time</strong> each machine takes to complete a process.</p>\n\n<p>The time to complete a process is the <code>'end' timestamp</code> minus the <code>'start' timestamp</code>. 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.</p>\n\n<p>The resulting table should have the <code>machine_id</code> along with the <strong>average time</strong> as <code>processing_time</code>, which should be <strong>rounded to 3 decimal places</strong>.</p>\n\n<p>Return the result table in <strong>any order</strong>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nActivity table:\n+------------+------------+---------------+-----------+\n| machine_id | process_id | activity_type | timestamp |\n+------------+------------+---------------+-----------+\n| 0 | 0 | start | 0.712 |\n| 0 | 0 | end | 1.520 |\n| 0 | 1 | start | 3.140 |\n| 0 | 1 | end | 4.120 |\n| 1 | 0 | start | 0.550 |\n| 1 | 0 | end | 1.550 |\n| 1 | 1 | start | 0.430 |\n| 1 | 1 | end | 1.420 |\n| 2 | 0 | start | 4.100 |\n| 2 | 0 | end | 4.512 |\n| 2 | 1 | start | 2.500 |\n| 2 | 1 | end | 5.000 |\n+------------+------------+---------------+-----------+\n<strong>Output:</strong> \n+------------+-----------------+\n| machine_id | processing_time |\n+------------+-----------------+\n| 0 | 0.894 |\n| 1 | 0.995 |\n| 2 | 1.456 |\n+------------+-----------------+\n<strong>Explanation:</strong> \nThere are 3 machines running 2 processes each.\nMachine 0's average time is ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894\nMachine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995\nMachine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456\n</pre>\n",
|
||
"translatedTitle": "每台机器的进程平均运行时间",
|
||
"translatedContent": "<p>表: <code>Activity</code></p>\n\n<pre>\n+----------------+---------+\n| Column Name | Type |\n+----------------+---------+\n| machine_id | int |\n| process_id | int |\n| activity_type | enum |\n| timestamp | float |\n+----------------+---------+\n该表展示了一家工厂网站的用户活动。\n(machine_id, process_id, activity_type) 是当前表的主键(具有唯一值的列的组合)。\nmachine_id 是一台机器的ID号。\nprocess_id 是运行在各机器上的进程ID号。\nactivity_type 是枚举类型 ('start', 'end')。\ntimestamp 是浮点类型,代表当前时间(以秒为单位)。\n'start' 代表该进程在这台机器上的开始运行时间戳 , 'end' 代表该进程在这台机器上的终止运行时间戳。\n同一台机器,同一个进程都有一对开始时间戳和结束时间戳,而且开始时间戳永远在结束时间戳前面。</pre>\n\n<p> </p>\n\n<p>现在有一个工厂网站由几台机器运行,每台机器上运行着 <strong>相同数量的进程</strong> 。编写解决方案,计算每台机器各自完成一个进程任务的平均耗时。</p>\n\n<p>完成一个进程任务的时间指进程的<code>'end' 时间戳</code> 减去 <code>'start' 时间戳</code>。平均耗时通过计算每台机器上所有进程任务的总耗费时间除以机器上的总进程数量获得。</p>\n\n<p>结果表必须包含<code>machine_id(机器ID)</code> 和对应的 <strong>average time(平均耗时)</strong> 别名 <code>processing_time</code>,且<strong>四舍五入保留3位小数。</strong></p>\n\n<p>以 <strong>任意顺序</strong> 返回表。</p>\n\n<p>具体参考例子如下。</p>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong>\nActivity table:\n+------------+------------+---------------+-----------+\n| machine_id | process_id | activity_type | timestamp |\n+------------+------------+---------------+-----------+\n| 0 | 0 | start | 0.712 |\n| 0 | 0 | end | 1.520 |\n| 0 | 1 | start | 3.140 |\n| 0 | 1 | end | 4.120 |\n| 1 | 0 | start | 0.550 |\n| 1 | 0 | end | 1.550 |\n| 1 | 1 | start | 0.430 |\n| 1 | 1 | end | 1.420 |\n| 2 | 0 | start | 4.100 |\n| 2 | 0 | end | 4.512 |\n| 2 | 1 | start | 2.500 |\n| 2 | 1 | end | 5.000 |\n+------------+------------+---------------+-----------+\n<strong>输出:</strong>\n+------------+-----------------+\n| machine_id | processing_time |\n+------------+-----------------+\n| 0 | 0.894 |\n| 1 | 0.995 |\n| 2 | 1.456 |\n+------------+-----------------+\n<strong>解释:</strong>\n一共有3台机器,每台机器运行着两个进程.\n机器 0 的平均耗时: ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894\n机器 1 的平均耗时: ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995\n机器 2 的平均耗时: ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456</pre>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Easy",
|
||
"likes": 110,
|
||
"dislikes": 0,
|
||
"isLiked": null,
|
||
"similarQuestions": "[]",
|
||
"contributors": [],
|
||
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
|
||
"topicTags": [
|
||
{
|
||
"name": "Database",
|
||
"slug": "database",
|
||
"translatedName": "数据库",
|
||
"__typename": "TopicTagNode"
|
||
}
|
||
],
|
||
"companyTagStats": null,
|
||
"codeSnippets": [
|
||
{
|
||
"lang": "MySQL",
|
||
"langSlug": "mysql",
|
||
"code": "# Write your MySQL query statement below",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "MS SQL Server",
|
||
"langSlug": "mssql",
|
||
"code": "/* Write your T-SQL query statement below */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "Oracle",
|
||
"langSlug": "oraclesql",
|
||
"code": "/* Write your PL/SQL query statement below */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "Pandas",
|
||
"langSlug": "pythondata",
|
||
"code": "import pandas as pd\n\ndef get_average_time(activity: pd.DataFrame) -> pd.DataFrame:\n ",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "PostgreSQL",
|
||
"langSlug": "postgresql",
|
||
"code": "-- Write your PostgreSQL query statement below",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"26K\", \"totalSubmission\": \"37.7K\", \"totalAcceptedRaw\": 25978, \"totalSubmissionRaw\": 37686, \"acRate\": \"68.9%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "{\"headers\":{\"Activity\":[\"machine_id\",\"process_id\",\"activity_type\",\"timestamp\"]},\"rows\":{\"Activity\":[[0,0,\"start\",0.712],[0,0,\"end\",1.52],[0,1,\"start\",3.14],[0,1,\"end\",4.12],[1,0,\"start\",0.55],[1,0,\"end\",1.55],[1,1,\"start\",0.43],[1,1,\"end\",1.42],[2,0,\"start\",4.1],[2,0,\"end\",4.512],[2,1,\"start\",2.5],[2,1,\"end\",5]]}}",
|
||
"metaData": "{\"mysql\":[\"Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float)\"],\"mssql\":[\"create table Activity (machine_id int, process_id int, activity_type varchar(15) not null check(activity_type in ('start', 'end')), timestamp float)\"],\"oraclesql\":[\"create table Activity (machine_id int, process_id int, activity_type varchar(15) not null check(activity_type in ('start', 'end')), timestamp float)\"],\"database\":true,\"name\":\"get_average_time\",\"pythondata\":[\"Activity = pd.DataFrame([], columns=['machine_id', 'process_id', 'activity_type', 'timestamp']).astype({'machine_id':'Int64', 'process_id':'Int64', 'activity_type':'object', 'timestamp':'Float64'})\"],\"postgresql\":[\"Create table If Not Exists Activity (machine_id int, process_id int, activity_type VARCHAR(30) CHECK (activity_type IN ('start', 'end')), timestamp float)\\n\"],\"database_schema\":{\"Activity\":{\"machine_id\":\"INT\",\"process_id\":\"INT\",\"activity_type\":\"ENUM('start', 'end')\",\"timestamp\":\"FLOAT\"}}}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [
|
||
"Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float)",
|
||
"Truncate table Activity",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'start', '0.712')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'end', '1.52')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'start', '3.14')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'end', '4.12')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'start', '0.55')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'end', '1.55')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'start', '0.43')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'end', '1.42')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'start', '4.1')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'end', '4.512')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'start', '2.5')",
|
||
"insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'end', '5')"
|
||
],
|
||
"enableRunCode": true,
|
||
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}",
|
||
"book": null,
|
||
"isSubscribed": false,
|
||
"isDailyQuestion": false,
|
||
"dailyRecordStatus": null,
|
||
"editorType": "CKEDITOR",
|
||
"ugcQuestionId": null,
|
||
"style": "LEETCODE",
|
||
"exampleTestcases": "{\"headers\":{\"Activity\":[\"machine_id\",\"process_id\",\"activity_type\",\"timestamp\"]},\"rows\":{\"Activity\":[[0,0,\"start\",0.712],[0,0,\"end\",1.52],[0,1,\"start\",3.14],[0,1,\"end\",4.12],[1,0,\"start\",0.55],[1,0,\"end\",1.55],[1,1,\"start\",0.43],[1,1,\"end\",1.42],[2,0,\"start\",4.1],[2,0,\"end\",4.512],[2,1,\"start\",2.5],[2,1,\"end\",5]]}}",
|
||
"__typename": "QuestionNode"
|
||
}
|
||
}
|
||
} |