【Breadth-first Search】690. Employee Importance(easy)

#week4#

#from leetcode#

Description



You are given a data structure of employee information, which includes the employee‘s unique id, his importance value and his directsubordinates‘ 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.

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.

Analysis



每一个employee看做一个节点,有权值importance——点集

employee到自己的直接下属有一条边edge——边集

然后利用BFS从id的employee开始遍历自己的下属,同时用total保存importance的和

最终的total值便是结果

Code


#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    int getImportance(vector<Employee*> employees, int id) {
        int size = employees.size();
        int* employees_value = new int[size+1];
        vector<pair<int, int>> edge;
        queue<int> que;
        bool* hasDone = new bool[size+1];
        int total;
        // 遍历所有的employees
        // 得到边的集合以及点的value
        for (vector<Employee*>::iterator i = employees.begin();
            i != employees.end(); i++) {
            Employee* employee = (*i);
            cout << employee->id;
            hasDone[employee->id] = false;
            employees_value[employee->id] = employee->importance;
            if (!(employee->subordinates).empty())
                for (int j = 0; j < (employee->subordinates).size(); j++)
                    edge.push_back(make_pair(employee->id, employee->subordinates[j]));
        }

        // bfs
        que.push(id);
        hasDone[id] = true;
        total = employees_value[id];
        while (!que.empty()) {
            int top = que.front();
            que.pop();
            for (vector<pair<int, int>>::iterator i = edge.begin(); i != edge.end(); i++) {
                if ((*i).first == top && !(hasDone[(*i).second])) {
                    int j = ((*i).second);
                    que.push(j);
                    hasDone[j] = true;
                    total += employees_value[j];
                }
            }
        }
        return total;
    }
};

Runtime: 15 ms

时间: 2024-07-31 22:22:53

【Breadth-first Search】690. Employee Importance(easy)的相关文章

【Divide and Conquer】53.Maximum Subarray(easy)

#week2# #from leetcode# Description Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum

【Divide and Conquer】169. Majority Element(easy)

#Week_1# #From LeetCode# Description: Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in t

Python之路【第三篇】:Python基础(二)

Python之路[第三篇]:Python基础(二) 内置函数 一 详细见python文档,猛击这里 文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open. 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作.

【C语言学习】指针再学习(二)之数组的指针

★一维数组 一个整型数据为4个字节.4个字节就是32位,即可以表示2^32个数字 在程序中定义一个数组a[5] = {1,2,3,4,5}; 那么a[0]的地址就是0x00000000,数组名a是数组首元素的地址,a的地址也是0x00000000.a+1则表示的地址是0x00000004,而不是0x00000001.因为1这个值,根据前面的指针a所指向的类型的长度来调整自己的长度.也就是说如果a是指向整型的指针,那么后面加的1也表示4个字节,如果a是指向字符型的指针,那么后面加的1表示1个字节.

【C语言学习】指针再理解(一)

★内存和地址 内存的基本单位是字节,那么内存就可以按字节进行编址,就是给组成内存的所有连续的字节安排地址.其中一个最恰当的比喻就是房子与门牌号,假设有20间连续的房子,从最左边(或者左右边)开始分配门牌号1~20,给内存中字节分配的地址就相当于门牌号,字节就相当于房间.字节的容量只能存储一个字符.那么,内存是如何存储比字符更大的数据呢,比如:整型数据.浮点型数据? ★更大的内存单元 为了存储更大的数据,就会把两个字节或者跟多的字节合在一起组成更大的内存单元.比如内存可以以"字"为单位来

wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示

目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客户端[JDAgent桌面助手]开发详解(二)桌面宠物制作详解 因为前段时候有很多的事情 比较忙,自从上次写完博客之后很久没有更新了. 用WPF制作的京东桌面助手.这个作品是参加比赛的,自己花费了很多心思和时间在里面,最终的作品效果和比赛的结果还是令人满意的. 作品感觉不说很fashion,也足够细致

Python之路【第五篇】:Python基础(20)——模块、序列化、os模块

常用模块 time模块 time.time() import time import datetime print(time.time()) # 返回当前时间的时间戳 time.ctime() print(time.ctime()) # 将时间戳转化为字符串格式Wed Feb 17 11:41:27 2016,默认是当前系统时间的时间戳 print(time.ctime(time.time()-3600)) # ctime可以接收一个时间戳作为参数,返回该时间戳的字符串形式 Wed Feb 17

【第三篇】:Python基础(二)

函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议使用函数. 标注:不能死记, 函数可以理解为一个一个的功能块,你把一个大的功能拆分成一块一块的,用某项功能的时候就去调用某个功能块即可! 函数可以理解为:乐高积木,给你一块一块的,你可以用这些积木块组成你想要的任何,功能! 函数可以调用函数!主函数的作用就是把函数进行串联.调用!函数本身是不能自己执

【机器学习详解】SMO算法剖析(转载)

[机器学习详解]SMO算法剖析 转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/51227754 CSDN?勿在浮沙筑高台 本文力求简化SMO的算法思想,毕竟自己理解有限,无奈还是要拿一堆公式推来推去,但是静下心看完本篇并随手推导,你会迎刃而解的.推荐参看SMO原文中的伪代码. 1.SMO概念 上一篇博客已经详细介绍了SVM原理,为了方便求解,把原始最优化问题转化成了其对偶问题,因为对偶问题是一个凸二次规划问题,这样的凸二次规