Skip to content

Commit

Permalink
add 25 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
2xiao committed Dec 20, 2024
1 parent a737791 commit 96483f5
Show file tree
Hide file tree
Showing 64 changed files with 4,120 additions and 172 deletions.
134 changes: 78 additions & 56 deletions assets/output/0997.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ one of these people is secretly the town judge.

If the town judge exists, then:

1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties **1** and **2**.
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties **1** and **2**.

You are given an array `trust` where `trust[i] = [ai, bi]` representing that
the person labeled `ai` trusts the person labeled `bi`. If a trust
Expand All @@ -35,119 +35,141 @@ does not exist.
Return _the label of the town judge if the town judge exists and can be
identified, or return_`-1` _otherwise_.



**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
**Constraints:**

* `1 <= n <= 1000`
* `0 <= trust.length <= 10^4`
* `trust[i].length == 2`
* All the pairs of `trust` are **unique**.
* `ai != bi`
* `1 <= ai, bi <= n`

- `1 <= n <= 1000`
- `0 <= trust.length <= 10^4`
- `trust[i].length == 2`
- All the pairs of `trust` are **unique**.
- `ai != bi`
- `1 <= ai, bi <= n`

## 题目大意

小镇里有 `n` 个人,按从 `1``n` 的顺序编号。传言称,这些人中有一个暗地里是小镇法官。

如果小镇法官真的存在,那么:

1. 小镇法官不会信任任何人。
2. 每个人(除了小镇法官)都信任这位小镇法官。
3. 只有一个人同时满足属性 **1** 和属性 **2**
1. 小镇法官不会信任任何人。
2. 每个人(除了小镇法官)都信任这位小镇法官。
3. 只有一个人同时满足属性 **1** 和属性 **2**

给你一个数组 `trust` ,其中 `trust[i] = [ai, bi]` 表示编号为 `ai` 的人信任编号为 `bi` 的人。

如果小镇法官存在并且可以确定他的身份,请返回该法官的编号;否则,返回 `-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
>
>
**提示:**

- `1 <= n <= 1000`
- `0 <= trust.length <= 10^4`
- `trust[i].length == 2`
- `trust` 中的所有`trust[i] = [ai, bi]` **互不相同**
- `ai != bi`
- `1 <= ai, bi <= n`

**提示:**
## 解题思路

* `1 <= n <= 1000`
* `0 <= trust.length <= 10^4`
* `trust[i].length == 2`
* `trust` 中的所有`trust[i] = [ai, bi]` **互不相同**
* `ai != bi`
* `1 <= ai, bi <= n`
我们需要判断是否存在一个满足以下条件的法官:

1. 法官**不信任任何人**
2. **所有人(除了法官)都信任法官**

## 解题思路
具体来说,如果某人是法官,他的**出度(信任别人)为 0**,同时他的**入度(被别人信任)为 `n - 1`**

1. 对于每个人,记录他信任的次数(出度)和被别人信任的次数(入度),初始化为 0。

2. **遍历数组 `trust` 更新入度和出度**

- 如果 `trust[i] = [a, b]`,表示 `a` 信任 `b`
- `a` 的出度加 1。
- `b` 的入度加 1。

3. **判断是否存在法官**

- 遍历所有人,找出同时满足以下条件的编号 `i`
- 入度为 `n - 1`
- 出度为 0。

4. 如果找到了这样的编号,则返回它;否则返回 `-1`

#### 复杂度分析

- **时间复杂度**`O()`
- **空间复杂度**`O()`
- **时间复杂度**`O(n + m)`

- 遍历 `trust` 数组更新入度和出度需要 `O(m)`,其中 `m``trust` 的长度。
- 遍历所有节点判断法官需要 `O(n)`,其中 `n` 是小镇人数。
- 总时间复杂度为 `O(n + m)`

- **空间复杂度**`O(n)`,需要存储入度和出度数组

## 代码

```javascript

/**
* @param {number} n
* @param {number[][]} trust
* @return {number}
*/
var findJudge = function (n, trust) {
if (n === 1 && trust.length === 0) return 1; // 特殊情况:只有一个人且没有信任关系

let inDegree = new Array(n + 1).fill(0); // 入度
let outDegree = new Array(n + 1).fill(0); // 出度

for (let [a, b] of trust) {
outDegree[a]++;
inDegree[b]++;
}

for (let i = 1; i <= n; i++) {
if (inDegree[i] === n - 1 && outDegree[i] === 0) {
return i;
}
}

return -1;
};
```

## 相关题目

<!-- prettier-ignore -->
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
| :------: | :------ | :------: | :------ | :------: | :------: |
| 277 | 搜寻名人 🔒 | | [``](/tag/graph.md) [`双指针`](/tag/two-pointers.md) [`交互`](/tag/interactive.md) | 🟠 | [🀄️](https://leetcode.cn/problems/find-the-celebrity) [🔗](https://leetcode.com/problems/find-the-celebrity) |
| 277 | 搜寻名人 🔒 | | [``](/tag/graph.md) [`双指针`](/tag/two-pointers.md) [`交互`](/tag/interactive.md) | 🟠 | [🀄️](https://leetcode.cn/problems/find-the-celebrity) [🔗](https://leetcode.com/problems/find-the-celebrity) |
26 changes: 25 additions & 1 deletion src/.vuepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,30 +503,53 @@ export default sidebar({
"children": [
"0901",
"0905",
"0908",
"0909",
"0914",
"0917",
"0918",
"0921",
"0922",
"0925",
"0929",
"0931",
"0933",
"0938",
"0941",
"0942",
"0944",
"0945",
"0946",
"0951",
"0953",
"0954",
"0958",
"0961",
"0962",
"0965",
"0973",
"0976",
"0977",
"0981",
"0986",
"0994"
"0989",
"0993",
"0994",
"0997",
"0999"
]
},
{
"text": "1000-1099",
"collapsible": true,
"children": [
"1002",
"1004",
"1005",
"1008",
"1009",
"1013",
"1018",
"1021",
"1047",
"1049",
Expand Down Expand Up @@ -707,6 +730,7 @@ export default sidebar({
"2405",
"2406",
"2410",
"2415",
"2416",
"2458",
"2461",
Expand Down
4 changes: 2 additions & 2 deletions src/book/two_pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
| 1099 | 小于 K 的两数之和 🔒 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`二分查找`](/tag/binary-search.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/two-sum-less-than-k) [🔗](https://leetcode.com/problems/two-sum-less-than-k) |
| 75 | 颜色分类 | [[]](/problem/0075.md) | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟠 | [🀄️](https://leetcode.cn/problems/sort-colors) [🔗](https://leetcode.com/problems/sort-colors) |
| 360 | 有序转化数组 🔒 | | [`数组`](/tag/array.md) [`数学`](/tag/math.md) [`双指针`](/tag/two-pointers.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/sort-transformed-array) [🔗](https://leetcode.com/problems/sort-transformed-array) |
| 977 | 有序数组的平方 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/squares-of-a-sorted-array) [🔗](https://leetcode.com/problems/squares-of-a-sorted-array) |
| 977 | 有序数组的平方 | [[]](/problem/0977.md) | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/squares-of-a-sorted-array) [🔗](https://leetcode.com/problems/squares-of-a-sorted-array) |
| 881 | 救生艇 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/boats-to-save-people) [🔗](https://leetcode.com/problems/boats-to-save-people) |
| 42 | 接雨水 | [[]](/problem/0042.md) | [``](/tag/stack.md) [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) `2+` | 🔴 | [🀄️](https://leetcode.cn/problems/trapping-rain-water) [🔗](https://leetcode.com/problems/trapping-rain-water) |
| 443 | 压缩字符串 | [[]](/problem/0443.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/string-compression) [🔗](https://leetcode.com/problems/string-compression) |
Expand All @@ -71,7 +71,7 @@
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
| :------: | :------ | :------: | :------ | :------: | :------: |
| 350 | 两个数组的交集 II | [[]](/problem/0350.md) | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`双指针`](/tag/two-pointers.md) `2+` | 🟢 | [🀄️](https://leetcode.cn/problems/intersection-of-two-arrays-ii) [🔗](https://leetcode.com/problems/intersection-of-two-arrays-ii) |
| 925 | 长按键入 | | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/long-pressed-name) [🔗](https://leetcode.com/problems/long-pressed-name) |
| 925 | 长按键入 | [[]](/problem/0925.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/long-pressed-name) [🔗](https://leetcode.com/problems/long-pressed-name) |
| 844 | 比较含退格的字符串 | [[]](/problem/0844.md) | [``](/tag/stack.md) [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/backspace-string-compare) [🔗](https://leetcode.com/problems/backspace-string-compare) |
| 1229 | 安排会议日程 🔒 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟠 | [🀄️](https://leetcode.cn/problems/meeting-scheduler) [🔗](https://leetcode.com/problems/meeting-scheduler) |
| 415 | 字符串相加 | [[]](/problem/0415.md) | [`数学`](/tag/math.md) [`字符串`](/tag/string.md) [`模拟`](/tag/simulation.md) | 🟢 | [🀄️](https://leetcode.cn/problems/add-strings) [🔗](https://leetcode.com/problems/add-strings) |
Expand Down
2 changes: 1 addition & 1 deletion src/plan/company_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ headerDepth: 0
| 138 | 随机链表的复制 | [[]](/problem/0138.md) | [`哈希表`](/tag/hash-table.md) [`链表`](/tag/linked-list.md) | 🟠 | [🀄️](https://leetcode.cn/problems/copy-list-with-random-pointer) [🔗](https://leetcode.com/problems/copy-list-with-random-pointer) | 19 |
| 124 | 二叉树中的最大路径和 | [[]](/problem/0124.md) | [``](/tag/tree.md) [`深度优先搜索`](/tag/depth-first-search.md) [`动态规划`](/tag/dynamic-programming.md) `1+` | 🔴 | [🀄️](https://leetcode.cn/problems/binary-tree-maximum-path-sum) [🔗](https://leetcode.com/problems/binary-tree-maximum-path-sum) | 19 |
| 1268 | 搜索推荐系统 | [[]](/problem/1268.md) | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
| 953 | 验证外星语词典 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
| 953 | 验证外星语词典 | [[]](/problem/0953.md) | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
| 973 | 最接近原点的 K 个点 | [[]](/problem/0973.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) `4+` | 🟠 | [🀄️](https://leetcode.cn/problems/k-closest-points-to-origin) [🔗](https://leetcode.com/problems/k-closest-points-to-origin) | 18 |
| 29 | 两数相除 | [[]](/problem/0029.md) | [`位运算`](/tag/bit-manipulation.md) [`数学`](/tag/math.md) | 🟠 | [🀄️](https://leetcode.cn/problems/divide-two-integers) [🔗](https://leetcode.com/problems/divide-two-integers) | 18 |
| 636 | 函数的独占时间 | | [``](/tag/stack.md) [`数组`](/tag/array.md) | 🟠 | [🀄️](https://leetcode.cn/problems/exclusive-time-of-functions) [🔗](https://leetcode.com/problems/exclusive-time-of-functions) | 18 |
Expand Down
Loading

0 comments on commit 96483f5

Please sign in to comment.