forked from apachecn/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
辛苦 | Merge pull request apachecn#284 from royIdoodle/master
add: 997 js solution added
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
docs/Algorithm/Leetcode/JavaScript/0997._Find_The_Town_Judge.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# 997. Find The Town Judge(找到小镇的法官) | ||
|
||
**<font color=green>难度: Easy</font>** | ||
|
||
## 刷题内容 | ||
|
||
> 原题连接 | ||
* https://leetcode-cn.com/problems/find-the-town-judge/ | ||
|
||
> 内容描述 | ||
在一个小镇里,按从 1 到 N 标记了 N 个人。传言称,这些人中有一个是小镇上的秘密法官。 | ||
|
||
如果小镇的法官真的存在,那么: | ||
|
||
1. 小镇的法官不相信任何人。 | ||
2. 每个人(除了小镇法官外)都信任小镇的法官。 | ||
3. 只有一个人同时满足属性 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. `1 <= N <= 1000` | ||
2. `trust.length <= 10000` | ||
3. `trust[i]` 是完全不同的 | ||
4. `trust[i][0] != trust[i][1]` | ||
5. `1 <= trust[i][0], trust[i][1] <= N` | ||
|
||
|
||
|
||
## 解题方案 | ||
|
||
> 思路 | ||
> **- 时间复杂度: O(N)** | ||
> | ||
> **- 空间复杂度: O(N)** | ||
利用`trustedMap`来存储“被信任者”的列表,数组下标代表村民的“标记”,数组元素的值代表“被多少人信任”。 | ||
|
||
利用`trustOtherMap`来存储“村民信任列表”,数组下标代表村民的“标记”,数组元素的值代表该村民“信任几个人”。 | ||
|
||
根据题目,`每个人(除了小镇法官外)都信任小镇的法官。`所以`trustedMap`中值为`N-1`的那个元素下标即是法官;但是`小镇的法官不相信任何人。`所以上一步得到的标记所在`trustOtherMap`的值一定是空。 | ||
|
||
> 执行用时 :**136 ms**, 在所有 JavaScript 提交中击败了**95.77%**的用户 | ||
> | ||
> 内存消耗 :**43.4 MB**, 在所有 JavaScript 提交中击败了**68.00%**的用户 | ||
代码: | ||
|
||
```javascript | ||
/** | ||
* @param {number} N | ||
* @param {number[][]} trust | ||
* @return {number} | ||
*/ | ||
var findJudge = function(N, trust) { | ||
let trustedMap = [] | ||
let trustOtherMap = [] | ||
if (N === 1 && trust.length === 0) { | ||
return 1 | ||
} | ||
trust.forEach(([person, trustedPerson]) => { | ||
if (trustedMap[trustedPerson]) { | ||
trustedMap[trustedPerson]++ | ||
} else { | ||
trustedMap[trustedPerson] = 1 | ||
} | ||
if (trustOtherMap[person]) { | ||
trustOtherMap[person]++ | ||
} else { | ||
trustOtherMap[person] = 1 | ||
} | ||
}) | ||
const trustedPerson = trustedMap.findIndex(i => i === (N - 1)) | ||
if (trustedPerson !== -1 && !trustOtherMap[trustedPerson]) { | ||
return trustedPerson | ||
} else { | ||
return -1 | ||
} | ||
}; | ||
``` | ||
|