mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-21 04:56:46 +08:00
97 lines
19 KiB
JSON
97 lines
19 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "3828",
|
||
"questionFrontendId": "3482",
|
||
"categoryTitle": "Database",
|
||
"boundTopicId": 3605805,
|
||
"title": "Analyze Organization Hierarchy",
|
||
"titleSlug": "analyze-organization-hierarchy",
|
||
"content": "<p>Table: <code>Employees</code></p>\n\n<pre>\n+----------------+---------+\n| Column Name | Type | \n+----------------+---------+\n| employee_id | int |\n| employee_name | varchar |\n| manager_id | int |\n| salary | int |\n| department | varchar |\n+----------------+----------+\nemployee_id is the unique key for this table.\nEach row contains information about an employee, including their ID, name, their manager's ID, salary, and department.\nmanager_id is null for the top-level manager (CEO).\n</pre>\n\n<p>Write a solution to analyze the organizational hierarchy and answer the following:</p>\n\n<ol>\n\t<li><strong>Hierarchy Levels:</strong> For each employee, determine their level in the organization (CEO is level <code>1</code>, employees reporting directly to the CEO are level <code>2</code>, and so on).</li>\n\t<li><strong>Team Size:</strong> For each employee who is a manager, count the total number of employees under them (direct and indirect reports).</li>\n\t<li><strong>Salary Budget:</strong> For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary).</li>\n</ol>\n\n<p>Return <em>the result table ordered by <em>the result ordered by <strong>level</strong> in <strong>ascending</strong> order, then by <strong>budget</strong> in <strong>descending</strong> order, and finally by <strong>employee_name</strong> in <strong>ascending</strong> order</em>.</em></p>\n\n<p><em>The result format is in the following example.</em></p>\n\n<p> </p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p>Employees table:</p>\n\n<pre class=\"example-io\">\n+-------------+---------------+------------+--------+-------------+\n| employee_id | employee_name | manager_id | salary | department |\n+-------------+---------------+------------+--------+-------------+\n| 1 | Alice | null | 12000 | Executive |\n| 2 | Bob | 1 | 10000 | Sales |\n| 3 | Charlie | 1 | 10000 | Engineering |\n| 4 | David | 2 | 7500 | Sales |\n| 5 | Eva | 2 | 7500 | Sales |\n| 6 | Frank | 3 | 9000 | Engineering |\n| 7 | Grace | 3 | 8500 | Engineering |\n| 8 | Hank | 4 | 6000 | Sales |\n| 9 | Ivy | 6 | 7000 | Engineering |\n| 10 | Judy | 6 | 7000 | Engineering |\n+-------------+---------------+------------+--------+-------------+\n</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+---------------+-------+-----------+--------+\n| employee_id | employee_name | level | team_size | budget |\n+-------------+---------------+-------+-----------+--------+\n| 1 | Alice | 1 | 9 | 84500 |\n| 3 | Charlie | 2 | 4 | 41500 |\n| 2 | Bob | 2 | 3 | 31000 |\n| 6 | Frank | 3 | 2 | 23000 |\n| 4 | David | 3 | 1 | 13500 |\n| 7 | Grace | 3 | 0 | 8500 |\n| 5 | Eva | 3 | 0 | 7500 |\n| 9 | Ivy | 4 | 0 | 7000 |\n| 10 | Judy | 4 | 0 | 7000 |\n| 8 | Hank | 4 | 0 | 6000 |\n+-------------+---------------+-------+-----------+--------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Organization Structure:</strong>\n\n\t<ul>\n\t\t<li>Alice (ID: 1) is the CEO (level 1) with no manager</li>\n\t\t<li>Bob (ID: 2) and Charlie (ID: 3) report directly to Alice (level 2)</li>\n\t\t<li>David (ID: 4), Eva (ID: 5) report to Bob, while Frank (ID: 6) and Grace (ID: 7) report to Charlie (level 3)</li>\n\t\t<li>Hank (ID: 8) reports to David, and Ivy (ID: 9) and Judy (ID: 10) report to Frank (level 4)</li>\n\t</ul>\n\t</li>\n\t<li><strong>Level Calculation:</strong>\n\t<ul>\n\t\t<li>The CEO (Alice) is at level 1</li>\n\t\t<li>Each subsequent level of management adds 1 to the level</li>\n\t</ul>\n\t</li>\n\t<li><strong>Team Size Calculation:</strong>\n\t<ul>\n\t\t<li>Alice has 9 employees under her (the entire company except herself)</li>\n\t\t<li>Bob has 3 employees (David, Eva, and Hank)</li>\n\t\t<li>Charlie has 4 employees (Frank, Grace, Ivy, and Judy)</li>\n\t\t<li>David has 1 employee (Hank)</li>\n\t\t<li>Frank has 2 employees (Ivy and Judy)</li>\n\t\t<li>Eva, Grace, Hank, Ivy, and Judy have no direct reports (team_size = 0)</li>\n\t</ul>\n\t</li>\n\t<li><strong>Budget Calculation:</strong>\n\t<ul>\n\t\t<li>Alice's budget: Her salary (12000) + all employees' salaries (72500) = 84500</li>\n\t\t<li>Charlie's budget: His salary (10000) + Frank's budget (23000) + Grace's salary (8500) = 41500</li>\n\t\t<li>Bob's budget: His salary (10000) + David's budget (13500) + Eva's salary (7500) = 31000</li>\n\t\t<li>Frank's budget: His salary (9000) + Ivy's salary (7000) + Judy's salary (7000) = 23000</li>\n\t\t<li>David's budget: His salary (7500) + Hank's salary (6000) = 13500</li>\n\t\t<li>Employees with no direct reports have budgets equal to their own salary</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li>The result is ordered first by level in ascending order</li>\n\t<li>Within the same level, employees are ordered by budget in descending order then by name in ascending order</li>\n</ul>\n</div>\n",
|
||
"translatedTitle": "分析组织层级",
|
||
"translatedContent": "<p>表:<code>Employees</code></p>\n\n<pre>\n+----------------+---------+\n| Column Name | Type | \n+----------------+---------+\n| employee_id | int |\n| employee_name | varchar |\n| manager_id | int |\n| salary | int |\n| department | varchar |\n+----------------+----------+\nemployee_id 是这张表的唯一主键。\n每一行包含关于一名员工的信息,包括他们的 ID,姓名,他们经理的 ID,薪水和部门。\n顶级经理(CEO)的 manager_id 是空的。\n</pre>\n\n<p>编写一个解决方案来分析组织层级并回答下列问题:</p>\n\n<ol>\n\t<li><strong>层级:</strong>对于每名员工,确定他们在组织中的层级(CEO 层级为 <code>1</code>,CEO 的直接下属员工层级为 <code>2</code>,以此类推)。</li>\n\t<li><strong>团队大小:</strong>对于每个是经理的员工,计算他们手下的(直接或间接下属)总员工数。</li>\n\t<li><strong>薪资预算:</strong>对于每个经理,计算他们控制的总薪资预算(所有手下员工的工资总和,包括间接下属,加上自己的工资)。</li>\n</ol>\n\n<p>返回结果表以 <strong>层级</strong> <strong>升序</strong> 排序,然后以预算 <strong>降序</strong> 排序,最后以 <strong>employee_name 升序 </strong>排序。</p>\n\n<p>结果格式如下所示。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>输入:</strong></p>\n\n<p>Employees 表:</p>\n\n<pre class=\"example-io\">\n+-------------+---------------+------------+--------+-------------+\n| employee_id | employee_name | manager_id | salary | department |\n+-------------+---------------+------------+--------+-------------+\n| 1 | Alice | null | 12000 | Executive |\n| 2 | Bob | 1 | 10000 | Sales |\n| 3 | Charlie | 1 | 10000 | Engineering |\n| 4 | David | 2 | 7500 | Sales |\n| 5 | Eva | 2 | 7500 | Sales |\n| 6 | Frank | 3 | 9000 | Engineering |\n| 7 | Grace | 3 | 8500 | Engineering |\n| 8 | Hank | 4 | 6000 | Sales |\n| 9 | Ivy | 6 | 7000 | Engineering |\n| 10 | Judy | 6 | 7000 | Engineering |\n+-------------+---------------+------------+--------+-------------+\n</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+---------------+-------+-----------+--------+\n| employee_id | employee_name | level | team_size | budget |\n+-------------+---------------+-------+-----------+--------+\n| 1 | Alice | 1 | 9 | 84500 |\n| 3 | Charlie | 2 | 4 | 41500 |\n| 2 | Bob | 2 | 3 | 31000 |\n| 6 | Frank | 3 | 2 | 23000 |\n| 4 | David | 3 | 1 | 13500 |\n| 7 | Grace | 3 | 0 | 8500 |\n| 5 | Eva | 3 | 0 | 7500 |\n| 9 | Ivy | 4 | 0 | 7000 |\n| 10 | Judy | 4 | 0 | 7000 |\n| 8 | Hank | 4 | 0 | 6000 |\n+-------------+---------------+-------+-----------+--------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>组织结构:</strong>\n\n\t<ul>\n\t\t<li>Alice(ID:1)是 CEO(层级 1)没有经理。</li>\n\t\t<li>Bob(ID:2)和 Charlie(ID:3)是 Alice 的直接下属(层级 2)</li>\n\t\t<li>David(ID:4),Eva(ID:5)从属于 Bob,而 Frank(ID:6)和 Grace(ID:7)从属于 Charlie(层级 3)</li>\n\t\t<li>Hank(ID:8)从属于 David,而 Ivy(ID:9)和 Judy(ID:10)从属于 Frank(层级 4)</li>\n\t</ul>\n\t</li>\n\t<li><strong>层级计算:</strong>\n\t<ul>\n\t\t<li>CEO(Alice)层级为 1</li>\n\t\t<li>每个后续的管理层级都会使层级数加 1</li>\n\t</ul>\n\t</li>\n\t<li><strong>团队大小计算:</strong>\n\t<ul>\n\t\t<li>Alice 手下有 9 个员工(除她以外的整个公司)</li>\n\t\t<li>Bob 手下有 3 个员工(David,Eva 和 Hank)</li>\n\t\t<li>Charlie 手下有 4 个员工(Frank,Grace,Ivy 和 Judy)</li>\n\t\t<li>David 手下有 1 个员工(Hank)</li>\n\t\t<li>Frank 手下有 2 个员工(Ivy 和 Judy)</li>\n\t\t<li>Eva,Grace,Hank,Ivy 和 Judy 没有直接下属(team_size = 0)</li>\n\t</ul>\n\t</li>\n\t<li><strong>预算计算:</strong>\n\t<ul>\n\t\t<li>Alice 的预算:她的工资(12000)+ 所有员工的工资(72500)= 84500</li>\n\t\t<li>Charlie 的预算:他的工资(10000)+ Frank 的预算(23000)+ Grace 的工资(8500)= 41500</li>\n\t\t<li>Bob 的预算:他的工资 (10000) + David 的预算(13500)+ Eva 的工资(7500)= 31000</li>\n\t\t<li>Frank 的预算:他的工资 (9000) + Ivy 的工资(7000)+ Judy 的工资(7000)= 23000</li>\n\t\t<li>David 的预算:他的工资 (7500) + Hank 的工资(6000)= 13500</li>\n\t\t<li>没有直接下属的员工的预算等于他们自己的工资。</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p><strong>注意:</strong></p>\n\n<ul>\n\t<li>结果先以层级升序排序</li>\n\t<li>在同一层级内,员工按预算降序排序,然后按姓名升序排序</li>\n</ul>\n</div>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Hard",
|
||
"likes": 5,
|
||
"dislikes": 0,
|
||
"isLiked": null,
|
||
"similarQuestions": "[]",
|
||
"contributors": [],
|
||
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python3\": false, \"python\": false, \"javascript\": false, \"typescript\": false, \"csharp\": false, \"c\": false, \"golang\": false, \"kotlin\": false, \"swift\": false, \"rust\": false, \"ruby\": false, \"php\": false, \"dart\": false, \"scala\": false, \"elixir\": false, \"erlang\": false, \"racket\": false, \"cangjie\": false, \"bash\": false, \"html\": false, \"pythonml\": false, \"react\": false, \"vanillajs\": false, \"mysql\": false, \"mssql\": false, \"postgresql\": false, \"oraclesql\": false, \"pythondata\": 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": "PostgreSQL",
|
||
"langSlug": "postgresql",
|
||
"code": "-- Write your PostgreSQL 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 analyze_organization_hierarchy(employees: pd.DataFrame) -> pd.DataFrame:\n ",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"883\", \"totalSubmission\": \"1.4K\", \"totalAcceptedRaw\": 883, \"totalSubmissionRaw\": 1361, \"acRate\": \"64.9%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "{\"headers\":{\"Employees\":[\"employee_id\",\"employee_name\",\"manager_id\",\"salary\",\"department\"]},\"rows\":{\"Employees\":[[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\"]]}}",
|
||
"metaData": "{\"mysql\":[\"CREATE TABLE if not exists Employees (\\n employee_id INT,\\n employee_name VARCHAR(100),\\n manager_id INT,\\n salary INT,\\n department VARCHAR(50)\\n)\"],\"mssql\":[\"CREATE TABLE Employees (\\n employee_id INT,\\n employee_name VARCHAR(100),\\n manager_id INT,\\n salary INT,\\n department VARCHAR(50)\\n)\"],\"oraclesql\":[\"CREATE TABLE Employees (\\n employee_id NUMBER,\\n employee_name VARCHAR2(100),\\n manager_id NUMBER,\\n salary NUMBER,\\n department VARCHAR2(50)\\n)\"],\"database\":true,\"name\":\"analyze_organization_hierarchy\",\"postgresql\":[\"CREATE TABLE IF NOT EXISTS Employees (\\n employee_id INT,\\n employee_name VARCHAR(100),\\n manager_id INT,\\n salary INT,\\n department VARCHAR(50)\\n);\\n\"],\"pythondata\":[\"Employees = pd.DataFrame(columns=[\\\"employee_id\\\", \\\"employee_name\\\", \\\"manager_id\\\", \\\"salary\\\", \\\"department\\\"]).astype({\\\"employee_id\\\": \\\"int\\\", \\\"employee_name\\\": \\\"string\\\", \\\"manager_id\\\": \\\"Int64\\\", \\\"salary\\\": \\\"int\\\", \\\"department\\\": \\\"string\\\"})\\n\"],\"database_schema\":{\"Employees\":{\"employee_id\":\"INT\",\"employee_name\":\"VARCHAR(100)\",\"manager_id\":\"INT\",\"salary\":\"INT\",\"department\":\"VARCHAR(50)\"}}}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [
|
||
"CREATE TABLE if not exists Employees (\n employee_id INT,\n employee_name VARCHAR(100),\n manager_id INT,\n salary INT,\n department VARCHAR(50)\n)",
|
||
"Truncate table Employees",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('1', 'Alice', NULL, '12000', 'Executive')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('2', 'Bob', '1', '10000', 'Sales')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('3', 'Charlie', '1', '10000', 'Engineering')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('4', 'David', '2', '7500', 'Sales')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('5', 'Eva', '2', '7500', 'Sales')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('6', 'Frank', '3', '9000', 'Engineering')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('7', 'Grace', '3', '8500', 'Engineering')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('8', 'Hank', '4', '6000', 'Sales')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('9', 'Ivy', '6', '7000', 'Engineering')",
|
||
"insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('10', 'Judy', '6', '7000', 'Engineering')"
|
||
],
|
||
"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\":{\"Employees\":[\"employee_id\",\"employee_name\",\"manager_id\",\"salary\",\"department\"]},\"rows\":{\"Employees\":[[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\"]]}}",
|
||
"__typename": "QuestionNode"
|
||
}
|
||
}
|
||
} |