Skip to content

Commit 8193d9d

Browse files
committed
+ problem 851
1 parent c27016f commit 8193d9d

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 851. Loud and Rich
2+
There is a group of `n` people labeled from `0` to `n - 1` where each person has a different amount of money and a different level of quietness.
3+
4+
You are given an array `richer` where <code>richer[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that <code>a<sub>i</sub></code> has more money than <code>b<sub>i</sub></code> and an integer array `quiet` where `quiet[i]` is the quietness of the <code>i<sup>th</sup></code> person. All the given data in richer are **logically correct** (i.e., the data will not lead you to a situation where `x` is richer than `y` and `y` is richer than `x` at the same time).
5+
6+
Return *an integer array* `answer` *where* `answer[x] = y` *if* `y` *is the least quiet person (that is, the person* `y` *with the smallest value of* `quiet[y]`*) among all people who definitely have equal to or more money than the person* `x`.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
11+
<strong>Output:</strong> [5,5,2,5,4,5,6,7]
12+
<strong>Explanation:</strong>
13+
answer[0] = 5.
14+
Person 5 has more money than 3, which has more money than 1, which has more money than 0.
15+
The only person who is quieter (has lower quiet[x]) is person 7, but it is not clear if they have more money than person 0.
16+
answer[7] = 7.
17+
Among all people that definitely have equal to or more money than person 7 (which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x]) is person 7.
18+
The other answers can be filled out with similar reasoning.
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> richer = [], quiet = [0]
24+
<strong>Output:</strong> [0]
25+
</pre>
26+
27+
#### Constraints:
28+
* `n == quiet.length`
29+
* `1 <= n <= 500`
30+
* `0 <= quiet[i] < n`
31+
* All the values of `quiet` are **unique**.
32+
* `0 <= richer.length <= n * (n - 1) / 2`
33+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
34+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
35+
* All the pairs of `richer` are **unique**.
36+
* The observations in `richer` are all logically consistent.
37+
38+
## Solutions (Rust)
39+
40+
### 1. Solution
41+
```Rust
42+
impl Solution {
43+
pub fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32> {
44+
let n = quiet.len();
45+
let mut richer_count = vec![0; n];
46+
let mut poorer_people = vec![vec![]; n];
47+
let mut people = vec![];
48+
let mut answer = (0..n as i32).collect::<Vec<_>>();
49+
50+
for pair in &richer {
51+
richer_count[pair[1] as usize] += 1;
52+
poorer_people[pair[0] as usize].push(pair[1] as usize);
53+
}
54+
55+
for i in 0..n {
56+
if richer_count[i] == 0 {
57+
people.push(i);
58+
}
59+
}
60+
61+
while let Some(x) = people.pop() {
62+
for &y in &poorer_people[x] {
63+
richer_count[y] -= 1;
64+
if richer_count[y] == 0 {
65+
people.push(y);
66+
}
67+
if quiet[answer[x] as usize] < quiet[answer[y] as usize] {
68+
answer[y] = answer[x];
69+
}
70+
}
71+
}
72+
73+
answer
74+
}
75+
}
76+
```
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 851. 喧闹和富有
2+
有一组 `n` 个人作为实验对象,从 `0``n - 1` 编号,其中每个人都有不同数目的钱,以及不同程度的安静值(quietness)。为了方便起见,我们将编号为 `x` 的人简称为 "person `x` "。
3+
4+
给你一个数组 `richer` ,其中 <code>richer[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示 person <code>a<sub>i</sub></code> 比 person <code>b<sub>i</sub></code> 更有钱。另给你一个整数数组 `quiet` ,其中 `quiet[i]` 是 person `i` 的安静值。`richer` 中所给出的数据 逻辑自洽(也就是说,在 person `x` 比 person `y` 更有钱的同时,不会出现 person `y` 比 person `x` 更有钱的情况 )。
5+
6+
现在,返回一个整数数组 `answer` 作为答案,其中 `answer[x] = y` 的前提是,在所有拥有的钱肯定不少于 person `x` 的人中,person `y` 是最不安静的人(也就是安静值 `quiet[y]` 最小的人)。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
11+
<strong>输出:</strong> [5,5,2,5,4,5,6,7]
12+
<strong>解释:</strong>
13+
answer[0] = 5,
14+
person 5 比 person 3 有更多的钱,person 3 比 person 1 有更多的钱,person 1 比 person 0 有更多的钱。
15+
唯一较为安静(有较低的安静值 quiet[x])的人是 person 7,
16+
但是目前还不清楚他是否比 person 0 更有钱。
17+
answer[7] = 7,
18+
在所有拥有的钱肯定不少于 person 7 的人中(这可能包括 person 3,4,5,6 以及 7),
19+
最安静(有较低安静值 quiet[x])的人是 person 7。
20+
其他的答案也可以用类似的推理来解释。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> richer = [], quiet = [0]
26+
<strong>输出:</strong> [0]
27+
</pre>
28+
29+
#### 提示:
30+
* `n == quiet.length`
31+
* `1 <= n <= 500`
32+
* `0 <= quiet[i] < n`
33+
* `quiet` 的所有值 **互不相同**
34+
* `0 <= richer.length <= n * (n - 1) / 2`
35+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
36+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
37+
* `richer` 中的所有数对 **互不相同**
38+
*`richer` 的观察在逻辑上是一致的
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
impl Solution {
45+
pub fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32> {
46+
let n = quiet.len();
47+
let mut richer_count = vec![0; n];
48+
let mut poorer_people = vec![vec![]; n];
49+
let mut people = vec![];
50+
let mut answer = (0..n as i32).collect::<Vec<_>>();
51+
52+
for pair in &richer {
53+
richer_count[pair[1] as usize] += 1;
54+
poorer_people[pair[0] as usize].push(pair[1] as usize);
55+
}
56+
57+
for i in 0..n {
58+
if richer_count[i] == 0 {
59+
people.push(i);
60+
}
61+
}
62+
63+
while let Some(x) = people.pop() {
64+
for &y in &poorer_people[x] {
65+
richer_count[y] -= 1;
66+
if richer_count[y] == 0 {
67+
people.push(y);
68+
}
69+
if quiet[answer[x] as usize] < quiet[answer[y] as usize] {
70+
answer[y] = answer[x];
71+
}
72+
}
73+
}
74+
75+
answer
76+
}
77+
}
78+
```
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32> {
3+
let n = quiet.len();
4+
let mut richer_count = vec![0; n];
5+
let mut poorer_people = vec![vec![]; n];
6+
let mut people = vec![];
7+
let mut answer = (0..n as i32).collect::<Vec<_>>();
8+
9+
for pair in &richer {
10+
richer_count[pair[1] as usize] += 1;
11+
poorer_people[pair[0] as usize].push(pair[1] as usize);
12+
}
13+
14+
for i in 0..n {
15+
if richer_count[i] == 0 {
16+
people.push(i);
17+
}
18+
}
19+
20+
while let Some(x) = people.pop() {
21+
for &y in &poorer_people[x] {
22+
richer_count[y] -= 1;
23+
if richer_count[y] == 0 {
24+
people.push(y);
25+
}
26+
if quiet[answer[x] as usize] < quiet[answer[y] as usize] {
27+
answer[y] = answer[x];
28+
}
29+
}
30+
}
31+
32+
answer
33+
}
34+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@
511511
[846][846l] |[Hand of Straights][846] |![rs]
512512
[848][848l] |[Shifting Letters][848] |![rs]
513513
[849][849l] |[Maximize Distance to Closest Person][849] |![rs]
514+
[851][851l] |[Loud and Rich][851] |![rs]
514515
[852][852l] |[Peak Index in a Mountain Array][852] |![rs]
515516
[853][853l] |[Car Fleet][853] |![rs]
516517
[856][856l] |[Score of Parentheses][856] |![rs]
@@ -1924,6 +1925,7 @@
19241925
[846]:Problemset/0846-Hand%20of%20Straights/README.md#846-hand-of-straights
19251926
[848]:Problemset/0848-Shifting%20Letters/README.md#848-shifting-letters
19261927
[849]:Problemset/0849-Maximize%20Distance%20to%20Closest%20Person/README.md#849-maximize-distance-to-closest-person
1928+
[851]:Problemset/0851-Loud%20and%20Rich/README.md#851-loud-and-rich
19271929
[852]:Problemset/0852-Peak%20Index%20in%20a%20Mountain%20Array/README.md#852-peak-index-in-a-mountain-array
19281930
[853]:Problemset/0853-Car%20Fleet/README.md#853-car-fleet
19291931
[856]:Problemset/0856-Score%20of%20Parentheses/README.md#856-score-of-parentheses
@@ -3336,6 +3338,7 @@
33363338
[846l]:https://leetcode.com/problems/hand-of-straights/
33373339
[848l]:https://leetcode.com/problems/shifting-letters/
33383340
[849l]:https://leetcode.com/problems/maximize-distance-to-closest-person/
3341+
[851l]:https://leetcode.com/problems/loud-and-rich/
33393342
[852l]:https://leetcode.com/problems/peak-index-in-a-mountain-array/
33403343
[853l]:https://leetcode.com/problems/car-fleet/
33413344
[856l]:https://leetcode.com/problems/score-of-parentheses/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@
511511
[846][846l] |[一手顺子][846] |![rs]
512512
[848][848l] |[字母移位][848] |![rs]
513513
[849][849l] |[到最近的人的最大距离][849] |![rs]
514+
[851][851l] |[喧闹和富有][851] |![rs]
514515
[852][852l] |[山脉数组的峰顶索引][852] |![rs]
515516
[853][853l] |[车队][853] |![rs]
516517
[856][856l] |[括号的分数][856] |![rs]
@@ -1924,6 +1925,7 @@
19241925
[846]:Problemset/0846-Hand%20of%20Straights/README_CN.md#846-一手顺子
19251926
[848]:Problemset/0848-Shifting%20Letters/README_CN.md#848-字母移位
19261927
[849]:Problemset/0849-Maximize%20Distance%20to%20Closest%20Person/README_CN.md#849-到最近的人的最大距离
1928+
[851]:Problemset/0851-Loud%20and%20Rich/README_CN.md#851-喧闹和富有
19271929
[852]:Problemset/0852-Peak%20Index%20in%20a%20Mountain%20Array/README_CN.md#852-山脉数组的峰顶索引
19281930
[853]:Problemset/0853-Car%20Fleet/README_CN.md#853-车队
19291931
[856]:Problemset/0856-Score%20of%20Parentheses/README_CN.md#856-括号的分数
@@ -3336,6 +3338,7 @@
33363338
[846l]:https://leetcode.cn/problems/hand-of-straights/
33373339
[848l]:https://leetcode.cn/problems/shifting-letters/
33383340
[849l]:https://leetcode.cn/problems/maximize-distance-to-closest-person/
3341+
[851l]:https://leetcode.cn/problems/loud-and-rich/
33393342
[852l]:https://leetcode.cn/problems/peak-index-in-a-mountain-array/
33403343
[853l]:https://leetcode.cn/problems/car-fleet/
33413344
[856l]:https://leetcode.cn/problems/score-of-parentheses/

0 commit comments

Comments
 (0)