当前位置:网站首页>【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)
边栏推荐
- MySQL的开发环境和测试环境有什么区别??
- [200 opencv routines] 213 Draw circle
- [Unity][ECS]学习笔记(一)
- Threads and processes
- 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
- An error is reported when uninstalling Oracle
- R语言plotly可视化:plotly可视化互相重叠的直方图(histogram)、在直方图的底部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
- MySQL cannot be opened. Flash back
- 【云驻共创】DWS告警服务DMS详细介绍和集群连接方式简介
- Chapter 3 stack and queue
猜你喜欢

PMP Exam key summary VI - chart arrangement

使用 ABAP 操作 Excel 的几种方法
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

PMP examination key summary VIII - monitoring process group (2)

Key summary V of PMP examination - execution process group

一文读懂 12种卷积方法(含1x1卷积、转置卷积和深度可分离卷积等)

增强 Jupyter Notebook 的功能,这里有四个妙招

用 Compose 实现个空调,为你的夏日带去清凉

Matplotlib属性及注解
![[200 opencv routines] 213 Draw circle](/img/8d/a771ea7008f84ae3a3cf41507448ec.png)
[200 opencv routines] 213 Draw circle
随机推荐
第三章 栈和队列
读取pdf图片并识别内容
Instant messaging and BS architecture simulation of TCP practical cases
What is the difference between MySQL development environment and test environment??
Restful style
Matplotlib attribute and annotation
Settings of gift giving module and other custom controls in one-to-one video chat system code
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
Ideal interface automation project
Unity AssetBundle asset packaging and asset loading
bye! IE browser, this route edge continues to go on for IE
Install using snap in opencloudos NET 6
Chapter 3 stack and queue
The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
再見!IE瀏覽器,這條路由Edge替IE繼續走下去
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
Adapter mode
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
【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
R语言plotly可视化:plotly可视化互相重叠的直方图(histogram)、在直方图的底部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots