Skip to content

Commit 5676944

Browse files
committed
Add solution and test-cases for problem 3318
1 parent f03226b commit 5676944

File tree

3 files changed

+92
-25
lines changed

3 files changed

+92
-25
lines changed

leetcode/3301-3400/3318.Find-X-Sum-of-All-K-Long-Subarrays-I/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3318.Find X-Sum of All K-Long Subarrays I][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 an array `nums` of `n` integers and two integers `k` and `x`.
5+
6+
The **x-sum** of an array is calculated by the following procedure:
7+
8+
- Count the occurrences of all elements in the array.
9+
- Keep only the occurrences of the top `x` most frequent elements. If two elements have the same number of occurrences, the element with the **bigger** value is considered more frequent.
10+
- Calculate the sum of the resulting array.
11+
12+
**Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array.
13+
14+
Return an integer array `answer` of length `n - k + 1` where `answer[i]` is the **x-sum** of the subarray `nums[i..i + k - 1]`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
19+
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
1420
15-
## 题意
16-
> ...
21+
Output: [6,10,12]
1722
18-
## 题解
23+
Explanation:
1924
20-
### 思路1
21-
> ...
22-
Find X-Sum of All K-Long Subarrays I
23-
```go
25+
For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.
26+
For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.
27+
For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.
2428
```
2529

30+
**Example 2:**
31+
32+
```
33+
Input: nums = [3,8,7,8,7,5], k = 2, x = 2
34+
35+
Output: [11,15,15,15,12]
36+
37+
Explanation:
38+
39+
Since k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].
40+
```
2641

2742
## 结语
2843

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

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
)
6+
7+
type ElementInfo struct {
8+
Value int
9+
Freq int
10+
}
11+
12+
func Solution(nums []int, k int, x int) []int {
13+
n := len(nums)
14+
if k == 0 || n < k {
15+
return []int{}
16+
}
17+
18+
answer := make([]int, n-k+1)
19+
20+
for i := 0; i <= n-k; i++ {
21+
freqMap := make(map[int]int)
22+
for j := i; j < i+k; j++ {
23+
freqMap[nums[j]]++
24+
}
25+
26+
var elements []ElementInfo
27+
for val, freq := range freqMap {
28+
elements = append(elements, ElementInfo{
29+
Value: val,
30+
Freq: freq,
31+
})
32+
}
33+
34+
sort.Slice(elements, func(a, b int) bool {
35+
if elements[a].Freq != elements[b].Freq {
36+
return elements[a].Freq > elements[b].Freq
37+
}
38+
return elements[a].Value > elements[b].Value
39+
})
40+
41+
currentXSum := 0
42+
43+
limit := x
44+
if len(elements) < x {
45+
limit = len(elements)
46+
}
47+
48+
for idx := 0; idx < limit; idx++ {
49+
element := elements[idx]
50+
currentXSum += element.Value * element.Freq
51+
}
52+
53+
answer[i] = currentXSum
54+
}
55+
56+
return answer
557
}

leetcode/3301-3400/3318.Find-X-Sum-of-All-K-Long-Subarrays-I/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k, c.x)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.inputs, c.k, c.x)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)