1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-05 23:41:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/originData/analyze-organization-hierarchy.json
2025-03-14 03:44:12 +08:00

90 lines
13 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&#39;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&nbsp;<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>&nbsp;</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&#39;s budget: Her salary (12000) + all employees&#39; salaries (72500) = 84500</li>\n\t\t<li>Charlie&#39;s budget: His salary (10000) + Frank&#39;s budget (23000) + Grace&#39;s salary (8500) = 41500</li>\n\t\t<li>Bob&#39;s budget: His salary (10000) + David&#39;s budget (13500) + Eva&#39;s salary (7500) = 31000</li>\n\t\t<li>Frank&#39;s budget: His salary (9000) + Ivy&#39;s salary (7000) + Judy&#39;s salary (7000) = 23000</li>\n\t\t<li>David&#39;s budget: His salary (7500) + Hank&#39;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": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}",
"topicTags": [],
"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": "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"
},
{
"lang": "PostgreSQL",
"langSlug": "postgresql",
"code": "-- Write your PostgreSQL query statement below",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"40\", \"totalSubmission\": \"47\", \"totalAcceptedRaw\": 40, \"totalSubmissionRaw\": 47, \"acRate\": \"85.1%\"}",
"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"
}
}
}