1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-12 08:55:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/分析组织层级 [analyze-organization-hierarchy].html
2025-09-29 14:48:40 +08:00

122 lines
5.3 KiB
HTML
Raw Permalink 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.

<p>表:<code>Employees</code></p>
<pre>
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
| salary | int |
| department | varchar |
+----------------+----------+
employee_id 是这张表的唯一主键。
每一行包含关于一名员工的信息,包括他们的 ID姓名他们经理的 ID薪水和部门。
顶级经理CEO的 manager_id 是空的。
</pre>
<p>编写一个解决方案来分析组织层级并回答下列问题:</p>
<ol>
<li><strong>层级:</strong>对于每名员工确定他们在组织中的层级CEO 层级为&nbsp;<code>1</code>CEO 的直接下属员工层级为&nbsp;<code>2</code>,以此类推)。</li>
<li><strong>团队大小:</strong>对于每个是经理的员工,计算他们手下的(直接或间接下属)总员工数。</li>
<li><strong>薪资预算:</strong>对于每个经理,计算他们控制的总薪资预算(所有手下员工的工资总和,包括间接下属,加上自己的工资)。</li>
</ol>
<p>返回结果表以 <strong>层级</strong>&nbsp;<strong>升序</strong>&nbsp;排序,然后以预算 <strong>降序</strong> 排序,最后以 <strong>employee_name&nbsp;升序 </strong>排序。</p>
<p>结果格式如下所示。</p>
<p>&nbsp;</p>
<p><strong class="example">示例:</strong></p>
<div class="example-block">
<p><strong>输入:</strong></p>
<p>Employees 表:</p>
<pre class="example-io">
+-------------+---------------+------------+--------+-------------+
| employee_id | employee_name | manager_id | salary | department |
+-------------+---------------+------------+--------+-------------+
| 1 | Alice | null | 12000 | Executive |
| 2 | Bob | 1 | 10000 | Sales |
| 3 | Charlie | 1 | 10000 | Engineering |
| 4 | David | 2 | 7500 | Sales |
| 5 | Eva | 2 | 7500 | Sales |
| 6 | Frank | 3 | 9000 | Engineering |
| 7 | Grace | 3 | 8500 | Engineering |
| 8 | Hank | 4 | 6000 | Sales |
| 9 | Ivy | 6 | 7000 | Engineering |
| 10 | Judy | 6 | 7000 | Engineering |
+-------------+---------------+------------+--------+-------------+
</pre>
<p><strong>输出:</strong></p>
<pre class="example-io">
+-------------+---------------+-------+-----------+--------+
| employee_id | employee_name | level | team_size | budget |
+-------------+---------------+-------+-----------+--------+
| 1 | Alice | 1 | 9 | 84500 |
| 3 | Charlie | 2 | 4 | 41500 |
| 2 | Bob | 2 | 3 | 31000 |
| 6 | Frank | 3 | 2 | 23000 |
| 4 | David | 3 | 1 | 13500 |
| 7 | Grace | 3 | 0 | 8500 |
| 5 | Eva | 3 | 0 | 7500 |
| 9 | Ivy | 4 | 0 | 7000 |
| 10 | Judy | 4 | 0 | 7000 |
| 8 | Hank | 4 | 0 | 6000 |
+-------------+---------------+-------+-----------+--------+
</pre>
<p><strong>解释:</strong></p>
<ul>
<li><strong>组织结构:</strong>
<ul>
<li>AliceID1是 CEO层级 1没有经理。</li>
<li>BobID2&nbsp;CharlieID3&nbsp;Alice 的直接下属(层级 2</li>
<li>DavidID4EvaID5从属于&nbsp;Bob而 FrankID6和 GraceID7从属于&nbsp;Charlie层级 3</li>
<li>HankID8从属于&nbsp;David而 IvyID9&nbsp;JudyID10从属于&nbsp;Frank层级 4</li>
</ul>
</li>
<li><strong>层级计算:</strong>
<ul>
<li>CEOAlice层级为 1</li>
<li>每个后续的管理层级都会使层级数加 1</li>
</ul>
</li>
<li><strong>团队大小计算:</strong>
<ul>
<li>Alice 手下有 9 个员工(除她以外的整个公司)</li>
<li>Bob 手下有 3 个员工DavidEva 和 Hank</li>
<li>Charlie 手下有 4 个员工FrankGraceIvy 和 Judy</li>
<li>David 手下有 1 个员工Hank</li>
<li>Frank 手下有 2 个员工Ivy 和 Judy</li>
<li>EvaGraceHankIvy 和 Judy 没有直接下属team_size = 0</li>
</ul>
</li>
<li><strong>预算计算:</strong>
<ul>
<li>Alice 的预算她的工资12000+ 所有员工的工资72500= 84500</li>
<li>Charlie 的预算他的工资10000+ Frank 的预算23000+ Grace 的工资8500= 41500</li>
<li>Bob 的预算:他的工资 (10000) + David&nbsp;的预算13500+ Eva&nbsp;的工资7500= 31000</li>
<li>Frank 的预算:他的工资 (9000) + Ivy 的工资7000+ Judy&nbsp;的工资7000= 23000</li>
<li>David 的预算:他的工资 (7500) + Hank 的工资6000= 13500</li>
<li>没有直接下属的员工的预算等于他们自己的工资。</li>
</ul>
</li>
</ul>
<p><strong>注意:</strong></p>
<ul>
<li>结果先以层级升序排序</li>
<li>在同一层级内,员工按预算降序排序,然后按姓名升序排序</li>
</ul>
</div>