1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-05 15:31:43 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/originData/find-covid-recovery-patients.json
2025-06-18 01:10:28 +08:00

106 lines
19 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"data": {
"question": {
"questionId": "3932",
"questionFrontendId": "3586",
"categoryTitle": "Database",
"boundTopicId": 3701001,
"title": "Find COVID Recovery Patients",
"titleSlug": "find-covid-recovery-patients",
"content": "<p>Table: <code>patients</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| patient_id | int |\n| patient_name| varchar |\n| age | int |\n+-------------+---------+\npatient_id is the unique identifier for this table.\nEach row contains information about a patient.\n</pre>\n\n<p>Table: <code>covid_tests</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| test_id | int |\n| patient_id | int |\n| test_date | date |\n| result | varchar |\n+-------------+---------+\ntest_id is the unique identifier for this table.\nEach row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.\n</pre>\n\n<p>Write a solution to find patients who have <strong>recovered from COVID</strong> - patients who tested positive but later tested negative.</p>\n\n<ul>\n\t<li>A patient is considered recovered if they have <strong>at least one</strong> <strong>Positive</strong> test followed by at least one <strong>Negative</strong> test on a <strong>later date</strong></li>\n\t<li>Calculate the <strong>recovery time</strong> in days as the <strong>difference</strong> between the <strong>first positive test</strong> and the <strong>first negative test</strong> after that <strong>positive test</strong></li>\n\t<li><strong>Only include</strong> patients who have both positive and negative test results</li>\n</ul>\n\n<p>Return <em>the result table ordered by </em><code>recovery_time</code><em> in <strong>ascending</strong> order, then by </em><code>patient_name</code><em> in <strong>ascending</strong> order</em>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p>patients table:</p>\n\n<pre class=\"example-io\">\n+------------+--------------+-----+\n| patient_id | patient_name | age |\n+------------+--------------+-----+\n| 1 | Alice Smith | 28 |\n| 2 | Bob Johnson | 35 |\n| 3 | Carol Davis | 42 |\n| 4 | David Wilson | 31 |\n| 5 | Emma Brown | 29 |\n+------------+--------------+-----+\n</pre>\n\n<p>covid_tests table:</p>\n\n<pre class=\"example-io\">\n+---------+------------+------------+--------------+\n| test_id | patient_id | test_date | result |\n+---------+------------+------------+--------------+\n| 1 | 1 | 2023-01-15 | Positive |\n| 2 | 1 | 2023-01-25 | Negative |\n| 3 | 2 | 2023-02-01 | Positive |\n| 4 | 2 | 2023-02-05 | Inconclusive |\n| 5 | 2 | 2023-02-12 | Negative |\n| 6 | 3 | 2023-01-20 | Negative |\n| 7 | 3 | 2023-02-10 | Positive |\n| 8 | 3 | 2023-02-20 | Negative |\n| 9 | 4 | 2023-01-10 | Positive |\n| 10 | 4 | 2023-01-18 | Positive |\n| 11 | 5 | 2023-02-15 | Negative |\n| 12 | 5 | 2023-02-20 | Negative |\n+---------+------------+------------+--------------+\n</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+------------+--------------+-----+---------------+\n| patient_id | patient_name | age | recovery_time |\n+------------+--------------+-----+---------------+\n| 1 | Alice Smith | 28 | 10 |\n| 3 | Carol Davis | 42 | 10 |\n| 2 | Bob Johnson | 35 | 11 |\n+------------+--------------+-----+---------------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Alice Smith (patient_id = 1):</strong>\n\n\t<ul>\n\t\t<li>First positive test: 2023-01-15</li>\n\t\t<li>First negative test after positive: 2023-01-25</li>\n\t\t<li>Recovery time: 25 - 15 = 10 days</li>\n\t</ul>\n\t</li>\n\t<li><strong>Bob Johnson (patient_id = 2):</strong>\n\t<ul>\n\t\t<li>First positive test: 2023-02-01</li>\n\t\t<li>Inconclusive test on 2023-02-05 (ignored for recovery calculation)</li>\n\t\t<li>First negative test after positive: 2023-02-12</li>\n\t\t<li>Recovery time: 12 - 1 = 11 days</li>\n\t</ul>\n\t</li>\n\t<li><strong>Carol Davis (patient_id = 3):</strong>\n\t<ul>\n\t\t<li>Had negative test on 2023-01-20 (before positive test)</li>\n\t\t<li>First positive test: 2023-02-10</li>\n\t\t<li>First negative test after positive: 2023-02-20</li>\n\t\t<li>Recovery time: 20 - 10 = 10 days</li>\n\t</ul>\n\t</li>\n\t<li><strong>Patients not included:</strong>\n\t<ul>\n\t\t<li>David Wilson (patient_id = 4): Only has positive tests, no negative test after positive</li>\n\t\t<li>Emma Brown (patient_id = 5): Only has negative tests, never tested positive</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>Output table is ordered by recovery_time in ascending order, and then by patient_name in ascending order.</p>\n</div>\n",
"translatedTitle": "寻找 COVID 康复患者",
"translatedContent": "<p>表:<code>patients</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| patient_id | int |\n| patient_name| varchar |\n| age | int |\n+-------------+---------+\npatient_id 是这张表的唯一主键。\n每一行表示一个患者的信息。\n</pre>\n\n<p>表:<code>covid_tests</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| test_id | int |\n| patient_id | int |\n| test_date | date |\n| result | varchar |\n+-------------+---------+\ntest_id 是这张表的唯一主键。\n每一行代表一个 COVID 检测结果。结果可以是阳性、阴性或不确定。\n</pre>\n\n<p>编写一个解决方案以找到从 COVID 中康复的患者——那些曾经检测呈阳性但后来检测呈阴性的患者。</p>\n\n<ul>\n\t<li>患者如果 <strong>至少有一次阳性</strong> 检测结果后,在&nbsp;<strong>之后的日期</strong> 至少有一次 <strong>阴性</strong> 检测结果,则被认为已康复。</li>\n\t<li>计算从 <strong>首次阳性检测</strong> 结果到 <strong>该阳性检测</strong> 后的 <strong>首次阴性检测结果</strong> 之间的 <strong>康复时间</strong>(以天为单位)</li>\n\t<li><strong>仅包括&nbsp;</strong>同时具有阳性及阴性检测结果的患者</li>\n</ul>\n\n<p>返回结果表以<em>&nbsp;</em><code>recovery_time</code><em> </em><strong>升序 </strong>排序,然后以<em>&nbsp;</em><code>patient_name</code><em> </em><strong>升序&nbsp;</strong>排序。</p>\n\n<p>结果格式如下所示。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>输入:</strong></p>\n\n<p>patients 表:</p>\n\n<pre class=\"example-io\">\n+------------+--------------+-----+\n| patient_id | patient_name | age |\n+------------+--------------+-----+\n| 1 | Alice Smith | 28 |\n| 2 | Bob Johnson | 35 |\n| 3 | Carol Davis | 42 |\n| 4 | David Wilson | 31 |\n| 5 | Emma Brown | 29 |\n+------------+--------------+-----+\n</pre>\n\n<p>covid_tests 表:</p>\n\n<pre class=\"example-io\">\n+---------+------------+------------+--------------+\n| test_id | patient_id | test_date | result |\n+---------+------------+------------+--------------+\n| 1 | 1 | 2023-01-15 | Positive |\n| 2 | 1 | 2023-01-25 | Negative |\n| 3 | 2 | 2023-02-01 | Positive |\n| 4 | 2 | 2023-02-05 | Inconclusive |\n| 5 | 2 | 2023-02-12 | Negative |\n| 6 | 3 | 2023-01-20 | Negative |\n| 7 | 3 | 2023-02-10 | Positive |\n| 8 | 3 | 2023-02-20 | Negative |\n| 9 | 4 | 2023-01-10 | Positive |\n| 10 | 4 | 2023-01-18 | Positive |\n| 11 | 5 | 2023-02-15 | Negative |\n| 12 | 5 | 2023-02-20 | Negative |\n+---------+------------+------------+--------------+\n</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+------------+--------------+-----+---------------+\n| patient_id | patient_name | age | recovery_time |\n+------------+--------------+-----+---------------+\n| 1 | Alice Smith | 28 | 10 |\n| 3 | Carol Davis | 42 | 10 |\n| 2 | Bob Johnson | 35 | 11 |\n+------------+--------------+-----+---------------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>Alice Smith (patient_id = 1):</strong>\n\n\t<ul>\n\t\t<li>首次阳性检测2023-01-15</li>\n\t\t<li>阳性检测后的首次阴性检测2023-01-25</li>\n\t\t<li>康复时间25 - 15 = 10 天</li>\n\t</ul>\n\t</li>\n\t<li><strong>Bob Johnson (patient_id = 2):</strong>\n\t<ul>\n\t\t<li>首次阳性检测2023-02-01</li>\n\t\t<li>测试结果不明确2023-02-05忽略计算康复时间</li>\n\t\t<li>阳性检测后的首次阴性检测2023-02-12</li>\n\t\t<li>康复时间12 - 1 = 11 天</li>\n\t</ul>\n\t</li>\n\t<li><strong>Carol Davis (patient_id = 3):</strong>\n\t<ul>\n\t\t<li>检测呈阴性2023-01-20在阳性检测前</li>\n\t\t<li>首次阳性检测2023-02-10</li>\n\t\t<li>阳性检测后的首次阴性检测2023-02-20</li>\n\t\t<li>康复时间20 - 10 = 10 天</li>\n\t</ul>\n\t</li>\n\t<li><strong>没有包含的患者:</strong>\n\t<ul>\n\t\t<li>David Wilsonpatient_id = 4只有阳性检测之后没有阴性检测。</li>\n\t\t<li>Emma Brownpatient_id = 5只有阴性检测从未有阳性检测。</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>输出表以 recovery_time 升序排序,然后以 patient_name 升序排序。</p>\n</div>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 0,
"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, \"cangjie\": 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 find_covid_recovery_patients(patients: pd.DataFrame, covid_tests: pd.DataFrame) -> pd.DataFrame:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "PostgreSQL",
"langSlug": "postgresql",
"code": "-- Write your PostgreSQL query statement below",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"102\", \"totalSubmission\": \"180\", \"totalAcceptedRaw\": 102, \"totalSubmissionRaw\": 180, \"acRate\": \"56.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"headers\":{\"patients\":[\"patient_id\",\"patient_name\",\"age\"],\"covid_tests\":[\"test_id\",\"patient_id\",\"test_date\",\"result\"]},\"rows\":{\"patients\":[[1,\"Alice Smith\",28],[2,\"Bob Johnson\",35],[3,\"Carol Davis\",42],[4,\"David Wilson\",31],[5,\"Emma Brown\",29]],\"covid_tests\":[[1,1,\"2023-01-15\",\"Positive\"],[2,1,\"2023-01-25\",\"Negative\"],[3,2,\"2023-02-01\",\"Positive\"],[4,2,\"2023-02-05\",\"Inconclusive\"],[5,2,\"2023-02-12\",\"Negative\"],[6,3,\"2023-01-20\",\"Negative\"],[7,3,\"2023-02-10\",\"Positive\"],[8,3,\"2023-02-20\",\"Negative\"],[9,4,\"2023-01-10\",\"Positive\"],[10,4,\"2023-01-18\",\"Positive\"],[11,5,\"2023-02-15\",\"Negative\"],[12,5,\"2023-02-20\",\"Negative\"]]}}",
"metaData": "{\"mysql\":[\"CREATE TABLE patients (\\n patient_id INT,\\n patient_name VARCHAR(255),\\n age INT\\n)\",\"CREATE TABLE covid_tests (\\n test_id INT,\\n patient_id INT,\\n test_date DATE,\\n result VARCHAR(50)\\n)\"],\"mssql\":[\"CREATE TABLE patients (\\n patient_id INT,\\n patient_name VARCHAR(255),\\n age INT\\n)\",\"CREATE TABLE covid_tests (\\n test_id INT,\\n patient_id INT,\\n test_date DATE,\\n result VARCHAR(50)\\n)\"],\"oraclesql\":[\"CREATE TABLE patients (\\n patient_id NUMBER,\\n patient_name VARCHAR2(255),\\n age NUMBER\\n)\",\"CREATE TABLE covid_tests (\\n test_id NUMBER,\\n patient_id NUMBER,\\n test_date DATE,\\n result VARCHAR2(50)\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_covid_recovery_patients\",\"postgresql\":[\"CREATE TABLE patients (\\n patient_id INTEGER,\\n patient_name VARCHAR(255),\\n age INTEGER\\n);\\n\",\"CREATE TABLE covid_tests (\\n test_id INTEGER,\\n patient_id INTEGER,\\n test_date DATE,\\n result VARCHAR(50)\\n);\\n\"],\"pythondata\":[\"patients = pd.DataFrame({\\n 'patient_id': pd.Series(dtype='int'),\\n 'patient_name': pd.Series(dtype='str'),\\n 'age': pd.Series(dtype='int')\\n})\",\"covid_tests = pd.DataFrame({\\n 'test_id': pd.Series(dtype='int'),\\n 'patient_id': pd.Series(dtype='int'),\\n 'test_date': pd.Series(dtype='datetime64[ns]'),\\n 'result': pd.Series(dtype='str')\\n})\"],\"database_schema\":{\"patients\":{\"patient_id\":\"INT\",\"patient_name\":\"VARCHAR(255)\",\"age\":\"INT\"},\"covid_tests\":{\"test_id\":\"INT\",\"patient_id\":\"INT\",\"test_date\":\"DATE\",\"result\":\"VARCHAR(50)\"}}}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [
"CREATE TABLE patients (\n patient_id INT,\n patient_name VARCHAR(255),\n age INT\n)",
"CREATE TABLE covid_tests (\n test_id INT,\n patient_id INT,\n test_date DATE,\n result VARCHAR(50)\n)",
"Truncate table patients",
"insert into patients (patient_id, patient_name, age) values ('1', 'Alice Smith', '28')",
"insert into patients (patient_id, patient_name, age) values ('2', 'Bob Johnson', '35')",
"insert into patients (patient_id, patient_name, age) values ('3', 'Carol Davis', '42')",
"insert into patients (patient_id, patient_name, age) values ('4', 'David Wilson', '31')",
"insert into patients (patient_id, patient_name, age) values ('5', 'Emma Brown', '29')",
"Truncate table covid_tests",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('1', '1', '2023-01-15', 'Positive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('2', '1', '2023-01-25', 'Negative')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('3', '2', '2023-02-01', 'Positive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('4', '2', '2023-02-05', 'Inconclusive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('5', '2', '2023-02-12', 'Negative')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('6', '3', '2023-01-20', 'Negative')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('7', '3', '2023-02-10', 'Positive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('8', '3', '2023-02-20', 'Negative')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('9', '4', '2023-01-10', 'Positive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('10', '4', '2023-01-18', 'Positive')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('11', '5', '2023-02-15', 'Negative')",
"insert into covid_tests (test_id, patient_id, test_date, result) values ('12', '5', '2023-02-20', 'Negative')"
],
"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.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"patients\":[\"patient_id\",\"patient_name\",\"age\"],\"covid_tests\":[\"test_id\",\"patient_id\",\"test_date\",\"result\"]},\"rows\":{\"patients\":[[1,\"Alice Smith\",28],[2,\"Bob Johnson\",35],[3,\"Carol Davis\",42],[4,\"David Wilson\",31],[5,\"Emma Brown\",29]],\"covid_tests\":[[1,1,\"2023-01-15\",\"Positive\"],[2,1,\"2023-01-25\",\"Negative\"],[3,2,\"2023-02-01\",\"Positive\"],[4,2,\"2023-02-05\",\"Inconclusive\"],[5,2,\"2023-02-12\",\"Negative\"],[6,3,\"2023-01-20\",\"Negative\"],[7,3,\"2023-02-10\",\"Positive\"],[8,3,\"2023-02-20\",\"Negative\"],[9,4,\"2023-01-10\",\"Positive\"],[10,4,\"2023-01-18\",\"Positive\"],[11,5,\"2023-02-15\",\"Negative\"],[12,5,\"2023-02-20\",\"Negative\"]]}}",
"__typename": "QuestionNode"
}
}
}