当前位置:网站首页>【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
2022-06-28 10:00:00 【亡心灵】
997. 找到小镇的法官
LeetCode: 997. 找到小镇的法官
简 单 \color{#00AF9B}{简单} 简单
在一个小镇里,按从
1到n为n个人进行编号。传言称,这些人中有一个是小镇上的秘密法官。
如果小镇的法官真的存在,那么:
- 小镇的法官不相信任何人。
- 每个人(除了小镇法官外)都信任小镇的法官。
- 只有一个人同时满足条件 1 和条件 2 。
给定数组
trust,该数组由信任对trust[i] = [a, b]组成,表示编号为a的人信任编号为b的人。
如果小镇存在秘密法官并且可以确定他的身份,请返回该法官的编号。否则,返回-1。
示例 1:
输入:n = 2, trust = [[1,2]]
输出:2
示例 2:
输入:n = 3, trust = [[1,3],[2,3]]
输出:3
示例 3:
输入:n = 3, trust = [[1,3],[2,3],[3,1]]
输出:-1
示例 4:
输入:n = 3, trust = [[1,2],[2,3]]
输出:-1
示例 5:
输入:n = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
输出:3
提示:
1 <= n <= 10000 <= trust.length <= 10^4trust[i].length == 2trust[i]互不相同trust[i][0] != trust[i][1]1 <= trust[i][0], trust[i][1] <= n
方法1:计算入度和出度
我们可以把 a 相信 b 看作是 a 向 b 的一条有向边,一个结点的入度即为有多少人相信此人,出度即为此人相信多少人。对于我们要找的法官,他不相信任何人,因此他的出度为0;所有人都相信他,因此入度为 n。我们只需要去找这样的结点即可。
#include <vector>
using namespace std;
class Solution
{
public:
int findJudge(int n, vector<vector<int>> &trust)
{
int *inDegrees = new int[n]{
}, *outDegrees = new int[n]{
};
for (const auto &edge : trust)
{
outDegrees[edge[0] - 1]++;
inDegrees[edge[1] - 1]++;
}
for (int i = 0; i < n; i++)
{
if (outDegrees[i] == 0 && inDegrees[i] == n - 1)
return i + 1;
}
return -1;
}
};
复杂度分析
时间复杂度: O ( m + n ) O(m+n) O(m+n)。其中,
m为数组trust的长度,n为人数。空间复杂度: O ( n ) O(n) O(n)。存放入度和出度各需要 O ( n ) O(n) O(n)的空间。
参考结果
Accepted
92/92 cases passed (164 ms)
Your runtime beats 42.06 % of cpp submissions
Your memory usage beats 75.85 % of cpp submissions (59.3 MB)
边栏推荐
- PMP examination key summary VIII - monitoring process group (2)
- 装饰模式(Decorator)
- 错过金三银四,找工作4个月,面试15家,终于拿到3个offer,定级P7+
- Solve the problem that the value of the action attribute of the form is null when transferring parameters
- Settings of gift giving module and other custom controls in one-to-one video chat system code
- 再见!IE浏览器,这条路由Edge替IE继续走下去
- Restful style
- Looking at jBPM from jbm3 to jbm5 and activiti
- Bron filter Course Research Report
- 如何查看谷歌浏览器保存的网页密码
猜你喜欢

Key summary V of PMP examination - execution process group

老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码

错过金三银四,找工作4个月,面试15家,终于拿到3个offer,定级P7+

缓存之王Caffeine Cache,性能比Guava更强
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)

解决表单action属性传参时值为null的问题

PMP examination key summary VIII - monitoring process group (2)
![[unity][ecs] learning notes (II)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[unity][ecs] learning notes (II)

装饰模式(Decorator)
随机推荐
2D code generator for openharmony application development
How to view the web password saved by Google browser
Composite pattern
在OpenCloudOS使用snap安装.NET 6
使用 ABAP 操作 Excel 的几种方法
如何查看谷歌浏览器保存的网页密码
bye! IE browser, this route edge continues to go on for IE
Thread lifecycle
JSON数据与List集合之间的正确转换
缓存之王Caffeine Cache,性能比Guava更强
Adapter mode
Au revoir! Navigateur ie, cette route Edge continue pour IE
Interface automation framework scaffolding - Implementation of parametric tools
[unity][ecs] learning notes (II)
The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
第三章 栈和队列
MySQL基础知识点总结
Django database operation and problem solving
Cisco * VRF (virtual route forwarding table)
Unity loads AssetBundle resources from the server and writes them to local memory, and loads the downloaded and saved AB resources from local memory to the scene