diff --git a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md index e3b471d11db24..8a2a10385c29e 100644 --- a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md +++ b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md @@ -73,11 +73,11 @@ tags: ### 方法一:栈 -我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 `"abc"`,所以每次插入操作之后,字符串的长度都会增加 $3$。如果字符串 $s$ 有效,那么它的长度一定是 $3$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 $3$ 的倍数,那么 $s$ 一定无效,可以直接返回 `false`。 +我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 $\textit{"abc"}$,所以每次插入操作之后,字符串的长度都会增加 $3$。如果字符串 $s$ 有效,那么它的长度一定是 $3$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 $3$ 的倍数,那么 $s$ 一定无效,可以直接返回 $\textit{false}$。 -接下来我们遍历字符串 $s$ 的每个字符 $c$,我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 $3$,并且栈顶的三个元素组成了字符串 `"abc"`,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。 +接下来我们遍历字符串 $s$ 的每个字符 $c$,我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 $3$,并且栈顶的三个元素组成了字符串 $\textit{"abc"}$,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。 -遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 `true`,否则返回 `false`。 +遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 $\textit{true}$;否则,返回 $\textit{false}$。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。 diff --git a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md index 448573b651d0d..e16aa8b35cebd 100644 --- a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md +++ b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md @@ -73,13 +73,13 @@ Thus, "abcabcababcc" is valid. ### Solution 1: Stack -If the string is valid, it's length must be the multiple of $3$. +We observe the operations in the problem and find that each time a string $\textit{"abc"}$ is inserted at any position in the string. Therefore, after each insertion operation, the length of the string increases by $3$. If the string $s$ is valid, its length must be a multiple of $3$. Thus, we first check the length of the string $s$. If it is not a multiple of $3$, then $s$ must be invalid, and we can directly return $\textit{false}$. -We traverse the string and push every character into the stack $t$. If the size of stack $t$ is greater than or equal to $3$ and the top three elements of stack $t$ constitute the string `"abc"`, we pop the top three elements. Then we continue to traverse the next character of the string $s$. +Next, we traverse each character $c$ in the string $s$. We first push the character $c$ onto the stack $t$. If the length of the stack $t$ is greater than or equal to $3$, and the top three elements of the stack form the string $\textit{"abc"}$, then we pop the top three elements from the stack. We then continue to traverse the next character in the string $s$. -When the traversal is over, if the stack $t$ is empty, the string $s$ is valid, return `true`, otherwise return `false`. +After the traversal, if the stack $t$ is empty, it means the string $s$ is valid, and we return $\textit{true}$; otherwise, we return $\textit{false}$. -The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the string $s$. +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$. diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md index dd8c718f55868..051e12d53965f 100644 --- a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md @@ -105,14 +105,56 @@ manager_id 是 employee_id 对应员工的经理。首席执行官的 manager_id -### 方法一 +### 方法一:递归 CTE + 连接 + +首先,我们使用递归 CTE 计算出每个员工的层级,其中 CEO 的层级为 0,将 `employee_id`、`employee_name`、`hierarchy_level`、`manager_id` 和 `salary` 保存到临时表 `T` 中。 + +然后,我们查询出 CEO 的薪资,将其保存到临时表 `P` 中。 + +最后,我们连接 `T` 和 `P` 表,计算出每个下属的薪资差异,并按照 `hierarchy_level` 和 `subordinate_id` 进行排序。 #### MySQL ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1; ``` diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md index df33c7678a355..cef9919f85541 100644 --- a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md @@ -106,14 +106,56 @@ manager_id is the employee_id of the employee's manager. The CEO has a NULL -### Solution 1 +### Solution 1: Recursive CTE + Join + +First, we use a recursive CTE to calculate the hierarchy level of each employee, where the CEO's level is $0$. We save `employee_id`, `employee_name`, `hierarchy_level`, `manager_id`, and `salary` into a temporary table `T`. + +Then, we query the CEO's salary and save it into a temporary table `P`. + +Finally, we join tables `T` and `P` to calculate the salary difference for each subordinate, and sort by `hierarchy_level` and `subordinate_id`. #### MySQL ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1; ``` diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql b/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql new file mode 100644 index 0000000000000..7c91b6608841c --- /dev/null +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql @@ -0,0 +1,37 @@ +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1;