Skip to content

Commit 94f444d

Browse files
authored
Merge pull request #1179 from 0xff-dev/1093
Add solution and test-cases for problem 1093
2 parents 0f24fe6 + f781a87 commit 94f444d

File tree

3 files changed

+111
-25
lines changed

3 files changed

+111
-25
lines changed

leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/README.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [1093.Statistics from a Large Sample][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+
You are given a large sample of integers in the range `[0, 255]`. Since the sample is so large, it is represented by an array `count` where `count[k]` is the **number of times** that `k` appears in the sample.
5+
6+
Calculate the following statistics:
7+
8+
- `minimum`: The minimum element in the sample.
9+
- `maximum`: The maximum element in the sample.
10+
- `mean`: The average of the sample, calculated as the total sum of all elements divided by the total number of elements.
11+
- `median`:
12+
13+
- If the sample has an odd number of elements, then the `median` is the middle element once the sample is sorted.
14+
- If the sample has an even number of elements, then the `median` is the average of the two middle elements once the sample is sorted.
15+
16+
- `mode`: The number that appears the most in the sample. It is guaranteed to be **unique**.
17+
18+
Return the statistics of the sample as an array of floating-point numbers `[minimum, maximum, mean, median, mode]`. Answers within `10^-5` of the actual answer will be accepted.
719

820
**Example 1:**
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
Input: count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
24+
Output: [1.00000,3.00000,2.37500,2.50000,3.00000]
25+
Explanation: The sample represented by count is [1,2,2,2,3,3,3,3].
26+
The minimum and maximum are 1 and 3 respectively.
27+
The mean is (1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375.
28+
Since the size of the sample is even, the median is the average of the two middle elements 2 and 3, which is 2.5.
29+
The mode is 3 as it appears the most in the sample.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 3:**
1933

20-
### 思路1
21-
> ...
22-
Statistics from a Large Sample
23-
```go
2434
```
25-
35+
Input: count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
36+
Output: [1.00000,4.00000,2.18182,2.00000,1.00000]
37+
Explanation: The sample represented by count is [1,1,1,1,2,2,2,3,3,4,4].
38+
The minimum and maximum are 1 and 4 respectively.
39+
The mean is (1+1+1+1+2+2+2+3+3+4+4) / 11 = 24 / 11 = 2.18181818... (for display purposes, the output shows the rounded number 2.18182).
40+
Since the size of the sample is odd, the median is the middle element 2.
41+
The mode is 1 as it appears the most in the sample.
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(count []int) []float64 {
4+
var _max, _min, _mode, _modeCount int
5+
_min = -1
6+
total := int64(0)
7+
sum := int64(0)
8+
9+
for i, c := range count {
10+
if c == 0 {
11+
continue
12+
}
13+
sum += int64(i) * int64(c)
14+
total += int64(c)
15+
16+
_max = max(_max, i)
17+
if _min == -1 || _min > i {
18+
_min = i
19+
}
20+
if c > _modeCount {
21+
_modeCount = c
22+
_mode = i
23+
}
24+
}
25+
_avg := float64(sum) / float64(total)
26+
var a float64
27+
start := total / 2
28+
if total&1 == 0 {
29+
start--
30+
}
31+
used := int64(0)
32+
justSelected := false
33+
for i, c := range count {
34+
if c == 0 {
35+
continue
36+
}
37+
if justSelected {
38+
a += float64(i)
39+
a /= 2
40+
break
41+
}
42+
next := used + int64(c)
43+
if next-1 < start {
44+
used = next
45+
continue
46+
}
47+
48+
diff := next - start
49+
a += float64(i)
50+
if total&1 == 1 {
51+
break
52+
}
53+
if diff >= 2 {
54+
a += a
55+
a /= 2
56+
break
57+
}
58+
justSelected = true
59+
}
60+
61+
return []float64{float64(_min), float64(_max), _avg, a, float64(_mode)}
562
}
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
package Solution
22

33
import (
4-
"reflect"
4+
"math"
55
"strconv"
66
"testing"
77
)
88

9+
const (
10+
epsilon = 1e-5
11+
)
12+
13+
func checkDiff(a, b []float64) bool {
14+
for i := range a {
15+
if !(math.Abs(a[i]-b[i]) < epsilon) {
16+
return false
17+
}
18+
}
19+
return true
20+
}
21+
922
func TestSolution(t *testing.T) {
1023
// 测试用例
1124
cases := []struct {
1225
name string
13-
inputs bool
14-
expect bool
26+
inputs []int
27+
expect []float64
1528
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
29+
{"TestCase1", []int{0, 1, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []float64{1.00000, 3.00000, 2.37500, 2.50000, 3.00000}},
30+
{"TestCase2", []int{0, 4, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []float64{1.00000, 4.00000, 2.18182, 2.00000, 1.00000}},
1931
}
2032

2133
// 开始测试
2234
for i, c := range cases {
2335
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
2436
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
37+
if !checkDiff(got, c.expect) {
2638
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2739
c.expect, got, c.inputs)
2840
}
2941
})
3042
}
3143
}
3244

33-
// 压力测试
45+
// 压力测试
3446
func BenchmarkSolution(b *testing.B) {
3547
}
3648

37-
// 使用案列
49+
// 使用案列
3850
func ExampleSolution() {
3951
}

0 commit comments

Comments
 (0)