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 ...
随机推荐
- Python+Selenium学习--设置等待时间
场景 sleep():设置固定休眠时间.python 的time 包提供了休眠方法sleep() ,导入time 包后就可以使用sleep()进行脚本的执行过程进行休眠.implicitly_wait ...
- windows phpstudy如何扩展MongoDB
phpstudy如何扩展MongoDB 作者: default|标签:phpstudy MongoDB PHP|2017-9-9 10:17 phpstudy扩展MongoDB 前置工作安装PHPst ...
- 【转】微信公众号h5网页被嵌入广告 不知道什么原因
这个是因为http劫持导致的.HTTP劫持是在使用者与其目的网络服务所建立的专用数据通道中,监视特定数据信息,提示当满足设定的条件时,就会在正常的数据流中插入精心设计的网络数据报文,目的是让用户端程序 ...
- selenium学习一
chrome版本和chromedriver的对应关系 chromedriver版本 支持的Chrome版本 v2.40 v66-68 v2.39 v66-68 v2.38 v65-67 v2.37 v ...
- 自定义进度条渐变色View
package com.jianke.stepCounter.Activity; import android.annotation.SuppressLint; import android.cont ...
- first H5
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- tiny4412 启动方式
1.iROM(BL0):是指Exynos4412的iROM中固化的启动代码,其作用是初始化系统时钟,设置看门狗,初始化堆和栈,加载8kb的bl1到Exynos4412的一个64kb大小内部sram(I ...
- JAVA虚拟机是?为什么称作是“平台无关的语言”?
Java虚拟机(Java Virtual Machine)简称JVM ,它是抽象化的计算机,有自己完善的硬体架构,如处理器.堆栈.寄存器等,还具有相应的指令系统.JVM屏蔽了与具体操作系统平台相关的信 ...
- js DomContentLoaded 和 load 的区别
如题:DOMContentLoaded和load都是页面加载的时候触发的事件.区别在于触发的时机不一样. 浏览器渲染页面DOM文档加载的步骤: 1.解析HTML结构. 2.加载外部脚本和css文件. ...
- u-boot之ARM920T的start.S分析
cpu/arm920t/start.S程序步骤大致有以下几个 1.设置中断向量表 2.设置CPU模式为SVC32 mode并且关闭IRQ与FIQ中断 3.关闭看门狗 4.屏蔽所有中断 5.判断程序是否 ...