LeetCode #690 EmployeeImportance. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스, sql, query, 쿼리
Runtime: 32 ms, faster than 67.94% of C++ online submissions for Employee Importance.
Memory Usage: 14.6 MB, less than 100.00% of C++ online submissions for Employee Importance.
LeetCode #690
Q.
You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.
For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.
Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.
직원 정보의 데이터스트럭쳐가 주어지고, 직원들의 유일한 id 가 있고, 그들의 중요도와 하위 직원들 id 리스트가 있다.
예를들면, 직원 1이 직원 2의 리더이고, 직원 2가 직원 3의 리더이다. 그들은 각각 중요도가 15, 10, 5 이다. 그러면 직원 1의 데이터스트럭처는 [1, 15, [2]] 로 주어지고, 직원 2는 [2, 10, [3]] 이고, 직원 3은 [3, 5, []] 이다. 직원 3은 직원 1의 하위 직원이지만 직접적인 관계는 없다.
이제 주어진 회사 직원들의 정보와 직원 id로, 그 id 의 모든 하위 직원들 중요도 합을 반환해라.
Example 1:
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
Explanation: Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
Note:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won't exceed 2000.
Process
// Process
//1. Input employees and start ID
//2. Make indexesList of all subordinates IDs
//3. Sum all of subordinates value
//4. Return importance sum
// 처리과정
//1. 직원들 정보와, 시작직원 ID 입력받는다.
//2. 직원들 정보에서의, 시작직원의 전체 하위직원들 id index를 만들어둔다.
//3. 모든 하위직원들 가치를 합한다.
//4. 합한 중요도가치를 반환한다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int sumOfImportance = 0;
vector<int> indexes;
// 2.
recursiveGetIDs(employees, id, indexes);
// 3.
for (int i = 0; i < indexes.size(); ++i) {
sumOfImportance += (employees.at(indexes.at(i))->importance);
}
// 4.
return sumOfImportance;
}
private:
void recursiveGetIDs(vector<Employee*> &employees, int id, vector<int> &indexes) {
int index = searchIndexID(employees, id);
indexes.push_back(index);
vector<int> subordinates = employees.at(index)->subordinates;
for (int i = 0; i < subordinates.size(); ++i) {
recursiveGetIDs(employees, subordinates.at(i), indexes);
}
}
int searchIndexID(vector<Employee*> &employees, int id) {
int index = -1;
for (int i = 0; i < employees.size(); ++i) {
if (employees.at(i)->id == id) {
index = i;
}
}
return index;
}
};
Something else you might like...?
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법