690. Employee Importance
好几种写法,这里贴几个出来
第一种:暴力解法,除去递归栈,空间复杂度O(1)。时间复杂度略高
/*
// 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;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
return getsum(employees,id);
} int getsum(vector<Employee*>employees,int id)
{
Employee * cur;
int cursum=;
for(auto p:employees)
{
if(p->id==id)
{
cur=p;
cursum+=p->importance;
break;
}
}
if(!(cur->subordinates.empty()))
{
for(auto subid:cur->subordinates)
cursum+=getsum(employees,subid);
}
return cursum;
}
};
第二种:用关联容器,速度快一丢丢,但是空间复杂度高一丢丢
/*
// 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;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
unordered_map<int,Employee*> iemap;
for(auto i:employees)
iemap[i->id]=i;
return getsum(iemap,id);
} int getsum(unordered_map<int,Employee*> & emap,int id)
{
int cursum=emap[id]->importance;
for(auto e:emap[id]->subordinates)
cursum+=getsum(emap,e);
return cursum;
}
};
第三种,用数组,空间复杂度比map高些,但是速度更快
/*
// 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;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
Employee* ptr[]={nullptr};
for(auto p:employees)
ptr[p->id]=p;
return getsum(ptr,id);
} int getsum(Employee* (&ptr)[],int id)
{
int cursum=ptr[id]->importance;
for(auto e:ptr[id]->subordinates)
cursum+=getsum(ptr,e);
return cursum;
}
};
三种方法都可以,都用了递归
690. Employee Importance的更多相关文章
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- [LeetCode&Python] Problem 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
随机推荐
- js导出excel:前端当前数据的导出
网上找的库文件,同样做了修改.在导出的时候,有时候数据第一列和最后一列可能是复选框和操作按钮,这个是我们不需要的,加了这个的过滤 //table2excel.js /* * jQuery table2 ...
- 像素 转换 px dp
public static int dip2px(Context context, float dpValue){ final float scale = context.getResources() ...
- Linux系统(Centos)下安装nodejs并配置环境
总结Centos下安装nodejs并配置环境,记录成功安装的方法.推荐的安装方法是利用已编译的二进制文件安装,不推荐使用源码的形式安装,一是源码安装比较麻烦,二是需要自行下载编译浪费时间. 1.安装n ...
- 截图原理(二)——android自动化测试学习历程
接上一篇(截图原理) 视频地址:http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=87 ...
- IOS是否存在32位和64位版本的区分
苹果于2013年9月推出了iPhone 5S新手机,采用的全新A7处理器其最大特色就是支持64位运算.其64位A7处理器的使用意味着iPhone性能会大有提高,性能和速度更加出色:而要到达到这样的性能 ...
- iOS开发时使用的bundle路径
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...
- 使用Nginx做转发和匹配替换
Nginx是一个强大的服务器软件,由于处理数据内容处于第七层协议应用层的原因,所以获取的数据也比较完整: Nginx做转发: 这个很简单,vi nginx.conf(编辑nginx配置文件) 添加lo ...
- saltstack 迭代项目到客户端并结合jenkins自动发布多台服务器
前面已经讲解了Webhook实现Push代码后的jenkins自动构建,接下来通过结合slatstack 实现多台机器的项目代码发布. 利用saltstack中file.recurse方法,运用该模块 ...
- (五)ROS节点
一. 理解ROS 节点: ROS的节点: 可以说是一个可运行的程序.当然这个程序可不简单.因为它可以接受来自ROS网络上其他可运行程序的输出信息,也可以发送信息给ROS网络,被其他 ROS 可运行程序 ...
- select into tb_temp2 from tb_temp1 创建临时表实现上一个、下一个功能,使用完毕就删除临时表
好久没有写过Sql了,今天遇到一个问题,业务逻辑是: 一个商品可以属于多个分类,在显示商品详情的时候,要求可以点击“上一个”,“下一个” 查看和该商品在同一个分类下的其他商品,商品具有排序号. 这样我 ...