690. Employee Importance

690. Employee Importance

/*
// Employee info
class Employee {
    // It's the unique id of each node;
    // unique id of this employee
    public int id;
    // the importance value of this employee
    public int importance;
    // the id of direct subordinates
    public List subordinates;
};
*/
class Solution {
	public Employee getEmployeeById(List employees, int id1) {
		for (int i = 0; i < employees.size(); i++) {
			if (employees.get(i).id == id1)
				return employees.get(i);
		}
		return null;
	}

	public int getImportance(List employees, int id) {
		int result = 0;
		Employee current = getEmployeeById(employees, id);
		result += current.importance;

		if (current.subordinates == null || current.subordinates.size() == 0)
			return result;

		for (int i = 0; i < current.subordinates.size(); i++)
			result += getImportance(employees, current.subordinates.get(i));

		return result;

	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s