본문 바로가기

Algorithm/Leet Code

LeetCode #690 EmployeeImportance. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터..

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:

  1. One employee has at most one direct leader and may have several subordinates.
  2. 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/11/02 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #7 MostExpensive. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, ..

2019/10/31 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #6 VolleyballResults. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

 

2019/10/20 - [Algorithm/Leet Code] - LeetCode #181 EmployeesEarningMoreTheirManagers. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

2019/10/20 - [Algorithm/Leet Code] - LeetCode #888 FairCandySwap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/10/14 - [Algorithm/Leet Code] - LeetCode #182 DuplicateEmails. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베..

 

 

2019/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법