Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fffbcb0

Browse files
feat: add solutions to lc problem: No.3236 (doocs#3347)
1 parent 62ac0a2 commit fffbcb0

File tree

5 files changed

+132
-11
lines changed

5 files changed

+132
-11
lines changed

‎solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ tags:
7373

7474
### 方法一:栈
7575

76-
我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 `"abc"`,所以每次插入操作之后,字符串的长度都会增加 3ドル$。如果字符串 $s$ 有效,那么它的长度一定是 3ドル$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 3ドル$ 的倍数,那么 $s$ 一定无效,可以直接返回 `false`
76+
我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 $\textit{"abc"}$,所以每次插入操作之后,字符串的长度都会增加 3ドル$。如果字符串 $s$ 有效,那么它的长度一定是 3ドル$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 3ドル$ 的倍数,那么 $s$ 一定无效,可以直接返回 $\textit{false}$
7777

78-
接下来我们遍历字符串 $s$ 的每个字符 $c,ドル我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 3ドル,ドル并且栈顶的三个元素组成了字符串 `"abc"`,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。
78+
接下来我们遍历字符串 $s$ 的每个字符 $c,ドル我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 3ドル,ドル并且栈顶的三个元素组成了字符串 $\textit{"abc"}$,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。
7979

80-
遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 `true`,否则返回 `false`
80+
遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 $\textit{true}$;否则,返回 $\textit{false}$
8181

8282
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
8383

‎solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ Thus, "abcabcababcc" is valid.
7373

7474
### Solution 1: Stack
7575

76-
If the string is valid, it's length must be the multiple of 3ドル$.
76+
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}$.
7777

78-
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$.
78+
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$.
7979

80-
When the traversal is over, if the stack $t$ is empty, the string $s$ is valid, return `true`, otherwisereturn `false`.
80+
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}$.
8181

82-
The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
82+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
8383

8484
<!-- tabs:start -->
8585

‎solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,56 @@ manager_id 是 employee_id 对应员工的经理。首席执行官的 manager_id
105105

106106
<!-- solution:start -->
107107

108-
### 方法一
108+
### 方法一:递归 CTE + 连接
109+
110+
首先,我们使用递归 CTE 计算出每个员工的层级,其中 CEO 的层级为 0,将 `employee_id``employee_name``hierarchy_level``manager_id``salary` 保存到临时表 `T` 中。
111+
112+
然后,我们查询出 CEO 的薪资,将其保存到临时表 `P` 中。
113+
114+
最后,我们连接 `T``P` 表,计算出每个下属的薪资差异,并按照 `hierarchy_level``subordinate_id` 进行排序。
109115

110116
<!-- tabs:start -->
111117

112118
#### MySQL
113119

114120
```sql
115-
121+
# Write your MySQL query statement below
122+
WITH RECURSIVE
123+
T AS (
124+
SELECT
125+
employee_id,
126+
employee_name,
127+
0 AS hierarchy_level,
128+
manager_id,
129+
salary
130+
FROM Employees
131+
WHERE manager_id IS NULL
132+
UNION ALL
133+
SELECT
134+
e.employee_id,
135+
e.employee_name,
136+
hierarchy_level + 1 AS hierarchy_level,
137+
e.manager_id,
138+
e.salary
139+
FROM
140+
T t
141+
JOIN Employees e ON t.employee_id = e.manager_id
142+
),
143+
P AS (
144+
SELECT salary
145+
FROM Employees
146+
WHERE manager_id IS NULL
147+
)
148+
SELECT
149+
employee_id subordinate_id,
150+
employee_name subordinate_name,
151+
hierarchy_level,
152+
t.salary - p.salary salary_difference
153+
FROM
154+
T t
155+
JOIN P p
156+
WHERE hierarchy_level != 0
157+
ORDER BY 3, 1;
116158
```
117159

118160
<!-- tabs:end -->

‎solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,56 @@ manager_id is the employee_id of the employee&#39;s manager. The CEO has a NULL
106106

107107
<!-- solution:start -->
108108

109-
### Solution 1
109+
### Solution 1: Recursive CTE + Join
110+
111+
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`.
112+
113+
Then, we query the CEO's salary and save it into a temporary table `P`.
114+
115+
Finally, we join tables `T` and `P` to calculate the salary difference for each subordinate, and sort by `hierarchy_level` and `subordinate_id`.
110116

111117
<!-- tabs:start -->
112118

113119
#### MySQL
114120

115121
```sql
116-
122+
# Write your MySQL query statement below
123+
WITH RECURSIVE
124+
T AS (
125+
SELECT
126+
employee_id,
127+
employee_name,
128+
0 AS hierarchy_level,
129+
manager_id,
130+
salary
131+
FROM Employees
132+
WHERE manager_id IS NULL
133+
UNION ALL
134+
SELECT
135+
e.employee_id,
136+
e.employee_name,
137+
hierarchy_level + 1 AS hierarchy_level,
138+
e.manager_id,
139+
e.salary
140+
FROM
141+
T t
142+
JOIN Employees e ON t.employee_id = e.manager_id
143+
),
144+
P AS (
145+
SELECT salary
146+
FROM Employees
147+
WHERE manager_id IS NULL
148+
)
149+
SELECT
150+
employee_id subordinate_id,
151+
employee_name subordinate_name,
152+
hierarchy_level,
153+
t.salary - p.salary salary_difference
154+
FROM
155+
T t
156+
JOIN P p
157+
WHERE hierarchy_level != 0
158+
ORDER BY 3, 1;
117159
```
118160

119161
<!-- tabs:end -->
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Write your MySQL query statement below
2+
WITH RECURSIVE
3+
T AS (
4+
SELECT
5+
employee_id,
6+
employee_name,
7+
0 AS hierarchy_level,
8+
manager_id,
9+
salary
10+
FROM Employees
11+
WHERE manager_id IS NULL
12+
UNION ALL
13+
SELECT
14+
e.employee_id,
15+
e.employee_name,
16+
hierarchy_level + 1 AS hierarchy_level,
17+
e.manager_id,
18+
e.salary
19+
FROM
20+
T t
21+
JOIN Employees e ON t.employee_id = e.manager_id
22+
),
23+
P AS (
24+
SELECT salary
25+
FROM Employees
26+
WHERE manager_id IS NULL
27+
)
28+
SELECT
29+
employee_id subordinate_id,
30+
employee_name subordinate_name,
31+
hierarchy_level,
32+
t.salary - p.salary salary_difference
33+
FROM
34+
T t
35+
JOIN P p
36+
WHERE hierarchy_level != 0
37+
ORDER BY 3, 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /