当前位置:网站首页>【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 <= 1000
0 <= trust.length <= 10^4
trust[i].length == 2
trust[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)
边栏推荐
- Summary of MySQL basic knowledge points
- Discard Tkinter! Simple configuration to quickly generate cool GUI!
- HDI blind hole design, have you noticed this detail?
- 引入 flink-sql-mysql-cdc-2.2.1 好多依赖冲突,有解决的吗?
- Dear leaders, ask me if MySQL does not support early_ Offset mode? Unsupported star
- 读取pdf文字和excel写入操作
- Idea failed to connect to SQL Sever
- As shown in the figure, the SQL row is used to convert the original table of Figure 1. Figure 2 wants to convert it
- 在OpenCloudOS使用snap安装.NET 6
- Google open source dependency injection framework Guice Guide
猜你喜欢
An error is reported when uninstalling Oracle
Discard Tkinter! Simple configuration to quickly generate cool GUI!
Solve the problem that the value of the action attribute of the form is null when transferring parameters
Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)
Key summary V of PMP examination - execution process group
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
bye! IE browser, this route edge continues to go on for IE
[200 opencv routines] 213 Draw circle
卸载oracle报错
随机推荐
再見!IE瀏覽器,這條路由Edge替IE繼續走下去
[unity][ecs] learning notes (I)
The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
Restful style
[Unity]EBUSY: resource busy or locked
Django database operation and problem solving
Unity AssetBundle资源打包与资源加载
Function sub file writing
Correct conversion between JSON data and list collection
Must the MySQL table have a primary key for incremental snapshots?
Solve the problem that the value of the action attribute of the form is null when transferring parameters
[Unity][ECS]学习笔记(二)
谁知道在中信建投证券开户是不是安全的
sqlcmd 连接数据库报错
==And eqauls()
Huawei OSPF single region
bad zipfile offset (local header sig)
JVM family (2) - garbage collection
The R language uses the avplots function in the car package to create added variable plots. In image interaction, manually identify (add) strong influence points that have a great impact on each predi
Google开源依赖注入框架-Guice指南