【LeetCode】997. Find the Town Judge 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/rotting-oranges/
题目描述
In a town, there are N
people labelled from 1
to N
. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
- The town judge trusts nobody.
- Everybody (except for the town judge) trusts the town judge.
- There is exactly one person that satisfies properties 1 and 2.
You are given trust
, an array of pairs trust[i] = [a, b]
representing that the person labelled a
trusts the person labelled b
.
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
Example 1:
Input: N = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: N = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Example 4:
Input: N = 3, trust = [[1,2],[2,3]]
Output: -1
Example 5:
Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3
Note:
- 1 <= N <= 1000
- trust.length <= 10000
- trust[i] are all different
- trust[i][0] != trusti
- 1 <= trust[i][0], trusti <= N
题目大意
镇里有编号1~N的N个人,如果有个法官存在的话,需要满足三个条件:
- 法官谁也不信任
- 每个人都信任这个人
- 只有一个人满足条件1和2
给出的trust[i] = [a, b]代表了a信任b。
如果法官存在,返回他的序号,否则返回-1.
解题方法
度
其实这个就是有向图,[a, b]表示从顶点a出发指向顶点b的一条有向边。
所以,题目的意思就是:是否存在且只存在一个顶点,所有的顶点都指向他,但是这个点不指向任何点。用术语来说就是该顶点的入度是N - 1,出度是0.
我们可以使用一个数组存储每个点的入度和出度的差
,当某个点的入度和出度的差
是N - 1时,代表他是法官,否则不存在。
证明:如果入度和出度的差
= N - 1,又入度、出度 >= 0,那么入度 = N- 1,出度 = 0,满足条件1和2.一旦存在一个点满足条件,那么说明这个点没有出度,所以不存在另一个点的入度是N - 1,满足条件3.
C++代码如下:
class Solution {
public:
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> g(N + 1, 0); // in-degree - out-degree
for (auto t : trust) {
++g[t[1]];
--g[t[0]];
}
for (int i = 1; i <= N; ++i) {
if (g[i] == N - 1)
return i;
}
return -1;
}
};
日期
2019 年 2 月 24 日 —— 周末又结束了
【LeetCode】997. Find the Town Judge 解题报告(C++)的更多相关文章
- #Leetcode# 997. Find the Town Judge
https://leetcode.com/problems/find-the-town-judge/ In a town, there are N people labelled from 1 to ...
- 【Leetcode_easy】997. Find the Town Judge
problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- 如何根据taxid(或taxname)快速获得taxname(或taxid)?
目录 需求 实现 需求 我有一个物种taxonomy ID的list,想获得相应的物种名,不要一个个去NCBI Taxonomy官网查.反之根据物种名list查询对应的taxid. 实现 因为之前没怎 ...
- 【机器学习与R语言】3-概率学习朴素贝叶斯(NB)
目录 1.理解朴素贝叶斯 1)基本概念 2)朴素贝叶斯算法 2.朴素贝斯分类应用 1)收集数据 2)探索和准备数据 3)训练模型 4)评估模型性能 5)提升模型性能 1.理解朴素贝叶斯 1)基本概念 ...
- tabix 操作VCF文件
tabix 可以对NGS分析中常见格式的文件建立索引,从而加快访问速度,不仅支持VCF文件,还支持BED, GFF,SAM等格式. 下载地址: 1 https://sourceforge.net/pr ...
- Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error
Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error 在安装目录下执行 vim obj/Markfile 把 ...
- Browse Code Answers
一个记录各种语言可能遇到的问题的论坛 :https://www.codegrepper.com/code-examples/
- pyspider爬虫框架的安装和使用
pyspider是国人binux编写的强大的网络爬虫框架,它带有强大的WebUI.脚本编辑器.任务监控器.项目管理器以及结果处理器,同时支持多种数据库后端.多种消息队列,另外还支持JavaScript ...
- Nginx编译参数详细介绍
/configure --help --help 显示本提示信息 --prefix=PATH 设定安装目录 --sbin-path=PATH 设定程序文件目录 --conf-path=PATH 设定配 ...
- (转载) IBM DB2数据库odbc配置步骤详解
[IT168 技术] 首先安装IBM DB2 odbc driver 1):可以单独下载DB2 Run-Time Client,大约(86.6m),安装后则odbc驱动程序安装成功.下载地址:ftp: ...
- Android editttext只能输入不能删除(选中后被软键盘遮住)
感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.
- nodeJs-querystring 模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js querystring 模块 GitHub TOP querystring 模块 来自<JavaScript 标准参考教 ...