返回介绍

solution / 1700-1799 / 1767.Find the Subtasks That Did Not Execute / README_EN

发布于 2024-06-17 01:03:14 字数 3421 浏览 0 评论 0 收藏 0

1767. Find the Subtasks That Did Not Execute

中文文档

Description

Table: Tasks

+----------------+---------+
| Column Name  | Type  |
+----------------+---------+
| task_id    | int   |
| subtasks_count | int   |
+----------------+---------+
task_id is the column with unique values for this table.
Each row in this table indicates that task_id was divided into subtasks_count subtasks labeled from 1 to subtasks_count.
It is guaranteed that 2 <= subtasks_count <= 20.

 

Table: Executed

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| task_id     | int   |
| subtask_id  | int   |
+---------------+---------+
(task_id, subtask_id) is the combination of columns with unique values for this table.
Each row in this table indicates that for the task task_id, the subtask with ID subtask_id was executed successfully.
It is guaranteed that subtask_id <= subtasks_count for each task_id.

 

Write a solution to report the IDs of the missing subtasks for each task_id.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Tasks table:
+---------+----------------+
| task_id | subtasks_count |
+---------+----------------+
| 1     | 3        |
| 2     | 2        |
| 3     | 4        |
+---------+----------------+
Executed table:
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1     | 2      |
| 3     | 1      |
| 3     | 2      |
| 3     | 3      |
| 3     | 4      |
+---------+------------+
Output: 
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1     | 1      |
| 1     | 3      |
| 2     | 1      |
| 2     | 2      |
+---------+------------+
Explanation: 
Task 1 was divided into 3 subtasks (1, 2, 3). Only subtask 2 was executed successfully, so we include (1, 1) and (1, 3) in the answer.
Task 2 was divided into 2 subtasks (1, 2). No subtask was executed successfully, so we include (2, 1) and (2, 2) in the answer.
Task 3 was divided into 4 subtasks (1, 2, 3, 4). All of the subtasks were executed successfully.

Solutions

Solution 1: Recursive Table Generation + Left Join

We can generate a table recursively that contains all pairs of (parent task, child task), and then use a left join to find the pairs that have not been executed.

# Write your MySQL query statement below
WITH RECURSIVE
  T(task_id, subtask_id) AS (
    SELECT
      task_id,
      subtasks_count
    FROM Tasks
    UNION ALL
    SELECT
      task_id,
      subtask_id - 1
    FROM t
    WHERE subtask_id > 1
  )
SELECT
  T.*
FROM
  T
  LEFT JOIN Executed USING (task_id, subtask_id)
WHERE Executed.subtask_id IS NULL;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文