Skip to content

Commit f9906ee

Browse files
authored
Merge pull request #820 from 0xff-dev/851
Add solution and test-cases for problem 851
2 parents 066b51b + be5c435 commit f9906ee

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

leetcode/801-900/0851.Loud-and-Rich/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [851.Loud and Rich][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
You are given an array `richer` where `richer[i] = [ai, bi]` indicates that a<sub>i</sub> has more money than b<sub>i</sub> and an integer array `quiet` where `quiet[i]` is the quietness of the i<sup>th</sup> 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).
7+
8+
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`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
14+
Output: [5,5,2,5,4,5,6,7]
15+
Explanation:
16+
answer[0] = 5.
17+
Person 5 has more money than 3, which has more money than 1, which has more money than 0.
18+
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.
19+
answer[7] = 7.
20+
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.
21+
The other answers can be filled out with similar reasoning.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Loud and Rich
23-
```go
2426
```
25-
27+
Input: richer = [], quiet = [0]
28+
Output: [0]
29+
```
2630

2731
## 结语
2832

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(richer [][]int, quiet []int) []int {
4+
l := len(quiet)
5+
ans := make([]int, l)
6+
greater := make([][]bool, l)
7+
for i := 0; i < l; i++ {
8+
greater[i] = make([]bool, l)
9+
}
10+
11+
for _, cmp := range richer {
12+
greater[cmp[1]][cmp[0]] = true
13+
}
14+
15+
cache := make(map[int]int)
16+
var dfs func(int) int
17+
dfs = func(x int) int {
18+
if v, ok := cache[x]; ok {
19+
return v
20+
}
21+
ans := x
22+
for u, v := range greater[x] {
23+
if v {
24+
if quiet[u] < quiet[ans] {
25+
ans = u
26+
}
27+
if r := dfs(u); quiet[ans] > quiet[r] {
28+
ans = r
29+
}
30+
}
31+
}
32+
cache[x] = ans
33+
return ans
34+
}
35+
for i := 0; i < l; i++ {
36+
ans[i] = dfs(i)
37+
}
38+
return ans
539
}

leetcode/801-900/0851.Loud-and-Rich/Solution_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
richer [][]int
14+
quiet []int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{
18+
{1, 0}, {2, 1}, {3, 1},
19+
{3, 7}, {4, 3}, {5, 3}, {6, 3},
20+
}, []int{3, 2, 5, 4, 6, 1, 7, 0}, []int{5, 5, 2, 5, 4, 5, 6, 7}},
21+
{"TestCase2", [][]int{}, []int{0}, []int{0}},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
27+
got := Solution(c.richer, c.quiet)
2528
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
30+
c.expect, got, c.richer, c.quiet)
2831
}
2932
})
3033
}
3134
}
3235

33-
// 压力测试
36+
// 压力测试
3437
func BenchmarkSolution(b *testing.B) {
3538
}
3639

37-
// 使用案列
40+
// 使用案列
3841
func ExampleSolution() {
3942
}

0 commit comments

Comments
 (0)