From f781a8703f9e7a0dfdd430ce138dc73138d2d327 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 18 Apr 2025 09:44:08 +0800 Subject: [PATCH] Add solution and test-cases for problem 1093 --- .../README.md | 45 +++++++++----- .../Solution.go | 61 ++++++++++++++++++- .../Solution_test.go | 30 ++++++--- 3 files changed, 111 insertions(+), 25 deletions(-) diff --git a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/README.md b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/README.md index b5dcd035a..ee4386054 100644 --- a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/README.md +++ b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/README.md @@ -1,28 +1,45 @@ # [1093.Statistics from a Large Sample][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. + +Calculate the following statistics: + +- `minimum`: The minimum element in the sample. +- `maximum`: The maximum element in the sample. +- `mean`: The average of the sample, calculated as the total sum of all elements divided by the total number of elements. +- `median`: + + - If the sample has an odd number of elements, then the `median` is the middle element once the sample is sorted. + - 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. + +- `mode`: The number that appears the most in the sample. It is guaranteed to be **unique**. + +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +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] +Output: [1.00000,3.00000,2.37500,2.50000,3.00000] +Explanation: The sample represented by count is [1,2,2,2,3,3,3,3]. +The minimum and maximum are 1 and 3 respectively. +The mean is (1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375. +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. +The mode is 3 as it appears the most in the sample. ``` -## 题意 -> ... - -## 题解 +**Example 3:** -### 思路1 -> ... -Statistics from a Large Sample -```go ``` - +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] +Output: [1.00000,4.00000,2.18182,2.00000,1.00000] +Explanation: The sample represented by count is [1,1,1,1,2,2,2,3,3,4,4]. +The minimum and maximum are 1 and 4 respectively. +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). +Since the size of the sample is odd, the median is the middle element 2. +The mode is 1 as it appears the most in the sample. +``` ## 结语 diff --git a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution.go b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution.go index d115ccf5e..56815aa2e 100644 --- a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution.go +++ b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution.go @@ -1,5 +1,62 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(count []int) []float64 { + var _max, _min, _mode, _modeCount int + _min = -1 + total := int64(0) + sum := int64(0) + + for i, c := range count { + if c == 0 { + continue + } + sum += int64(i) * int64(c) + total += int64(c) + + _max = max(_max, i) + if _min == -1 || _min > i { + _min = i + } + if c > _modeCount { + _modeCount = c + _mode = i + } + } + _avg := float64(sum) / float64(total) + var a float64 + start := total / 2 + if total&1 == 0 { + start-- + } + used := int64(0) + justSelected := false + for i, c := range count { + if c == 0 { + continue + } + if justSelected { + a += float64(i) + a /= 2 + break + } + next := used + int64(c) + if next-1 < start { + used = next + continue + } + + diff := next - start + a += float64(i) + if total&1 == 1 { + break + } + if diff >= 2 { + a += a + a /= 2 + break + } + justSelected = true + } + + return []float64{float64(_min), float64(_max), _avg, a, float64(_mode)} } diff --git a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution_test.go b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution_test.go index 14ff50eb4..f1200c5e7 100644 --- a/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution_test.go +++ b/leetcode/1001-1100/1093.Statistics-from-a-Large-Sample/Solution_test.go @@ -1,28 +1,40 @@ package Solution import ( - "reflect" + "math" "strconv" "testing" ) +const ( + epsilon = 1e-5 +) + +func checkDiff(a, b []float64) bool { + for i := range a { + if !(math.Abs(a[i]-b[i]) < epsilon) { + return false + } + } + return true +} + func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []float64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"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}}, + {"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}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { got := Solution(c.inputs) - if !reflect.DeepEqual(got, c.expect) { + if !checkDiff(got, c.expect) { t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs) } @@ -30,10 +42,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }