{ "data": { "question": { "questionId": "690", "questionFrontendId": "690", "categoryTitle": "Algorithms", "boundTopicId": 1118, "title": "Employee Importance", "titleSlug": "employee-importance", "content": "

You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.

\n\n

You are given an array of employees employees where:

\n\n\n\n

Given an integer id that represents an employee's ID, return the total importance value of this employee and all their direct and indirect subordinates.

\n\n

 

\n

Example 1:

\n\"\"\n
\nInput: employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1\nOutput: 11\nExplanation: Employee 1 has an importance value of 5 and has two direct subordinates: employee 2 and employee 3.\nThey both have an importance value of 3.\nThus, the total importance value of employee 1 is 5 + 3 + 3 = 11.\n
\n\n

Example 2:

\n\"\"\n
\nInput: employees = [[1,2,[5]],[5,-3,[]]], id = 5\nOutput: -3\nExplanation: Employee 5 has an importance value of -3 and has no direct subordinates.\nThus, the total importance value of employee 5 is -3.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "员工的重要性", "translatedContent": "

给定一个保存员工信息的数据结构,它包含了员工 唯一的 id 重要度 直系下属的 id

\n\n

比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] ,员工 2的 数据结构是 [2, 10, [3]] ,员工 3 的数据结构是 [3, 5, []] 。注意虽然员工 3 也是员工 1 的一个下属,但是由于 并不是直系 下属,因此没有体现在员工 1 的数据结构中。

\n\n

现在输入一个公司的所有员工信息,以及单个员工 id ,返回这个员工和他所有下属的重要度之和。

\n\n

 

\n\n

示例:

\n\n
\n输入:[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1\n输出:11\n解释:\n员工 1 自身的重要度是 5 ,他有两个直系下属 2 和 3 ,而且 2 和 3 的重要度均为 3 。因此员工 1 的总重要度是 5 + 3 + 3 = 11 。\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 286, "dislikes": 0, "isLiked": null, "similarQuestions": "[{\"title\": \"Nested List Weight Sum\", \"titleSlug\": \"nested-list-weight-sum\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u5d4c\\u5957\\u5217\\u8868\\u6743\\u91cd\\u548c\", \"isPaidOnly\": true}]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"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}", "topicTags": [ { "name": "Depth-First Search", "slug": "depth-first-search", "translatedName": "深度优先搜索", "__typename": "TopicTagNode" }, { "name": "Breadth-First Search", "slug": "breadth-first-search", "translatedName": "广度优先搜索", "__typename": "TopicTagNode" }, { "name": "Hash Table", "slug": "hash-table", "translatedName": "哈希表", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "/*\n// Definition for Employee.\nclass Employee {\npublic:\n int id;\n int importance;\n vector subordinates;\n};\n*/\n\nclass Solution {\npublic:\n int getImportance(vector employees, int id) {\n \n }\n};", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "/*\n// Definition for Employee.\nclass Employee {\n public int id;\n public int importance;\n public List subordinates;\n};\n*/\n\nclass Solution {\n public int getImportance(List employees, int id) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "\"\"\"\n# Definition for Employee.\nclass Employee(object):\n def __init__(self, id, importance, subordinates):\n \t#################\n :type id: int\n :type importance: int\n :type subordinates: List[int]\n #################\n self.id = id\n self.importance = importance\n self.subordinates = subordinates\n\"\"\"\n\nclass Solution(object):\n def getImportance(self, employees, id):\n \"\"\"\n :type employees: List[Employee]\n :type id: int\n :rtype: int\n \"\"\"\n ", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "\"\"\"\n# Definition for Employee.\nclass Employee:\n def __init__(self, id: int, importance: int, subordinates: List[int]):\n self.id = id\n self.importance = importance\n self.subordinates = subordinates\n\"\"\"\n\nclass Solution:\n def getImportance(self, employees: List['Employee'], id: int) -> int:\n ", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "/*\n// Definition for Employee.\nclass Employee {\n public int id;\n public int importance;\n public IList subordinates;\n}\n*/\n\nclass Solution {\n public int GetImportance(IList employees, int id) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * Definition for Employee.\n * function Employee(id, importance, subordinates) {\n * this.id = id;\n * this.importance = importance;\n * this.subordinates = subordinates;\n * }\n */\n\n/**\n * @param {Employee[]} employees\n * @param {number} id\n * @return {number}\n */\nvar GetImportance = function(employees, id) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "/**\n * Definition for Employee.\n * class Employee {\n * id: number\n * importance: number\n * subordinates: number[]\n * constructor(id: number, importance: number, subordinates: number[]) {\n * this.id = (id === undefined) ? 0 : id;\n * this.importance = (importance === undefined) ? 0 : importance;\n * this.subordinates = (subordinates === undefined) ? [] : subordinates;\n * }\n * }\n */\n\nfunction getImportance(employees: Employee[], id: number): number {\n\t\n};", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "/**\n * Definition for Employee.\n * class Employee {\n * public $id = null;\n * public $importance = null;\n * public $subordinates = array();\n * function __construct($id, $importance, $subordinates) {\n * $this->id = $id;\n * $this->importance = $importance;\n * $this->subordinates = $subordinates;\n * }\n * }\n */\n\nclass Solution {\n /**\n * @param Employee[] $employees\n * @param Integer $id\n * @return Integer\n */\n function getImportance($employees, $id) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "/**\n * Definition for Employee.\n * public class Employee {\n * public var id: Int\n * public var importance: Int\n * public var subordinates: [Int]\n * public init(_ id: Int, _ importance: Int, _ subordinates: [Int]) {\n * self.id = id\n * self.importance = importance\n * self.subordinates = subordinates\n * }\n * }\n */\n\nclass Solution {\n func getImportance(_ employees: [Employee], _ id: Int) -> Int {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "/*\n *\t// Definition for Employee.\n *\tclass Employee {\n *\t\tvar id:Int = 0\n *\t\tvar importance:Int = 0\n *\t\tvar subordinates:List = listOf()\n *\t}\n */\n\nclass Solution {\n fun getImportance(employees: List, id: Int): Int {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "/**\n * Definition for Employee.\n * type Employee struct {\n * Id int\n * Importance int\n * Subordinates []int\n * }\n */\n\nfunc getImportance(employees []*Employee, id int) int {\n \n}", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "=begin\n# Definition for Employee.\nclass Employee\n attr_accessor :id, :importance, :subordinates\n def initialize( id, importance, subordinates)\n @id = id\n @importance = importance\n @subordinates = subordinates\n end\nend\n=end\n\n# @param {Employee} employees\n# @param {Integer} id\n# @return {Integer}\ndef get_importance(employees, id)\n \nend", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "/*\n// Definition for Employee.\nclass Employee() {\n var id: Int = 0\n var importance: Int = 0\n var subordinates: List[Int] = List()\n};\n*/\n\nobject Solution {\n def getImportance(employees: List[Employee], id: Int): Int = {\n \n }\n}", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"67.3K\", \"totalSubmission\": \"102.1K\", \"totalAcceptedRaw\": 67256, \"totalSubmissionRaw\": 102053, \"acRate\": \"65.9%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[[1,5,[2,3]],[2,3,[]],[3,3,[]]]\n1", "metaData": "{\n \"name\": \"getImportance\",\n \"params\": [\n {\n \"name\": \"employees\",\n \"type\": \"list\"\n },\n {\n \"name\": \"id\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"cpp\",\n \"java\",\n \"python\",\n \"ruby\",\n \"python3\",\n \"kotlin\",\n \"scala\",\n \"csharp\",\n \"javascript\",\n \"swift\",\n \"golang\",\n \"php\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"cpp\":[\"C++\",\"

\\u7248\\u672c\\uff1aclang 11<\\/code> \\u91c7\\u7528\\u6700\\u65b0C++ 20\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n

\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528-O2<\\/code>\\u7ea7\\u4f18\\u5316\\u3002AddressSanitizer<\\/a> \\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4bout-of-bounds<\\/code>\\u548cuse-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\"],\"java\":[\"Java\",\"

\\u7248\\u672c\\uff1aOpenJDK 17<\\/code>\\u3002\\u53ef\\u4ee5\\u4f7f\\u7528Java 8\\u7684\\u7279\\u6027\\u4f8b\\u5982\\uff0clambda expressions \\u548c stream API\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u88ab\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5305\\u542b Pair \\u7c7b: https:\\/\\/docs.oracle.com\\/javase\\/8\\/javafx\\/api\\/javafx\\/util\\/Pair.html <\\/p>\"],\"python\":[\"Python\",\"

\\u7248\\u672c\\uff1a Python 2.7.12<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982\\uff1aarray<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u6ce8\\u610f Python 2.7 \\u5c06\\u57282020\\u5e74\\u540e\\u4e0d\\u518d\\u7ef4\\u62a4<\\/a>\\u3002 \\u5982\\u60f3\\u4f7f\\u7528\\u6700\\u65b0\\u7248\\u7684Python\\uff0c\\u8bf7\\u9009\\u62e9Python 3\\u3002<\\/p>\"],\"csharp\":[\"C#\",\"

C# 10<\\/a> \\u8fd0\\u884c\\u5728 .NET 6 \\u4e0a<\\/p>\"],\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"ruby\":[\"Ruby\",\"

\\u4f7f\\u7528Ruby 3.1<\\/code>\\u6267\\u884c<\\/p>\\r\\n\\r\\n

\\u4e00\\u4e9b\\u5e38\\u7528\\u7684\\u6570\\u636e\\u7ed3\\u6784\\u5df2\\u5728 Algorithms \\u6a21\\u5757\\u4e2d\\u63d0\\u4f9b\\uff1ahttps:\\/\\/www.rubydoc.info\\/github\\/kanwei\\/algorithms\\/Algorithms<\\/p>\"],\"swift\":[\"Swift\",\"

\\u7248\\u672c\\uff1aSwift 5.5.2<\\/code><\\/p>\\r\\n\\r\\n

\\u6211\\u4eec\\u901a\\u5e38\\u4fdd\\u8bc1\\u66f4\\u65b0\\u5230 Apple\\u653e\\u51fa\\u7684\\u6700\\u65b0\\u7248Swift<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u53d1\\u73b0Swift\\u4e0d\\u662f\\u6700\\u65b0\\u7248\\u7684\\uff0c\\u8bf7\\u8054\\u7cfb\\u6211\\u4eec\\uff01\\u6211\\u4eec\\u5c06\\u5c3d\\u5feb\\u66f4\\u65b0\\u3002<\\/p>\"],\"golang\":[\"Go\",\"

\\u7248\\u672c\\uff1aGo 1.21<\\/code><\\/p>\\r\\n\\r\\n

\\u652f\\u6301 https:\\/\\/godoc.org\\/github.com\\/emirpasic\\/gods@v1.18.1<\\/a> \\u7b2c\\u4e09\\u65b9\\u5e93\\u3002<\\/p>\"],\"python3\":[\"Python3\",\"

\\u7248\\u672c\\uff1aPython 3.10<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982array<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002 \\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528 Map\\/TreeMap \\u6570\\u636e\\u7ed3\\u6784\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 sortedcontainers<\\/a> \\u5e93\\u3002<\\/p>\"],\"scala\":[\"Scala\",\"

\\u7248\\u672c\\uff1aScala 2.13<\\/code><\\/p>\"],\"kotlin\":[\"Kotlin\",\"

\\u7248\\u672c\\uff1aKotlin 1.9.0<\\/code><\\/p>\\r\\n\\r\\n

\\u6211\\u4eec\\u4f7f\\u7528\\u7684\\u662f JetBrains \\u63d0\\u4f9b\\u7684 experimental compiler\\u3002\\u5982\\u679c\\u60a8\\u8ba4\\u4e3a\\u60a8\\u9047\\u5230\\u4e86\\u7f16\\u8bd1\\u5668\\u76f8\\u5173\\u7684\\u95ee\\u9898\\uff0c\\u8bf7\\u5411\\u6211\\u4eec\\u53cd\\u9988<\\/p>\"],\"php\":[\"PHP\",\"

PHP 8.1<\\/code>.<\\/p>\\r\\n\\r\\n

With bcmath module.<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 5.1.6<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "[[1,5,[2,3]],[2,3,[]],[3,3,[]]]\n1\n[[1,2,[5]],[5,-3,[]]]\n5", "__typename": "QuestionNode" } } }