当前位置:网站首页>【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)
边栏推荐
- [200 opencv routines] 213 Draw circle
- Teach you how to handle the reverse SVG mapping of JS
- 满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
- 如何查看谷歌浏览器保存的网页密码
- Sqlcmd database connection error
- 第五章 树和二叉树
- bad zipfile offset (local header sig)
- Function sub file writing
- Discard Tkinter! Simple configuration to quickly generate cool GUI!
- Sword finger offer | Fibonacci sequence
猜你喜欢

组合模式(Composite Pattern)

如图 用sql行转列 图一原表,图二希望转换后

Function sub file writing

第六天 脚本与动画系统

缓存之王Caffeine Cache,性能比Guava更强

再見!IE瀏覽器,這條路由Edge替IE繼續走下去

Proxy mode (proxy)

The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached

Explain final, finally, and finalize

解决表单action属性传参时值为null的问题
随机推荐
Application of X6 in data stack index management
缓存之王Caffeine Cache,性能比Guava更强
[200 opencv routines] 213 Draw circle
Global exception handlers and unified return results
sqlcmd 连接数据库报错
Au revoir! Navigateur ie, cette route Edge continue pour IE
PMP Exam key summary VI - chart arrangement
理想中的接口自动化项目
bad zipfile offset (local header sig)
谁知道在中信建投证券开户是不是安全的
Matplotlib属性及注解
标识符的命名规则和规范
股票开户用中金证券经理发的开户二维码安全吗?知道的给说一下吧
Django database operation and problem solving
Resolution: overview of decentralized hosting solution
Crawler small operation
Install using snap in opencloudos NET 6
PHP curl forged IP address and header information code instance - Alibaba cloud
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"