Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [3318.Find X-Sum of All K-Long Subarrays I][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 an array `nums` of `n` integers and two integers `k` and `x`.

The **x-sum** of an array is calculated by the following procedure:

- Count the occurrences of all elements in the array.
- 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.
- Calculate the sum of the resulting array.

**Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array.

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]`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2

## 题意
> ...
Output: [6,10,12]

## 题解
Explanation:

### 思路1
> ...
Find X-Sum of All K-Long Subarrays I
```go
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.
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.
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.
```

**Example 2:**

```
Input: nums = [3,8,7,8,7,5], k = 2, x = 2

Output: [11,15,15,15,12]

Explanation:

Since k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
)

type ElementInfo struct {
Value int
Freq int
}

func Solution(nums []int, k int, x int) []int {
n := len(nums)
if k == 0 || n < k {
return []int{}
}

answer := make([]int, n-k+1)

for i := 0; i <= n-k; i++ {
freqMap := make(map[int]int)
for j := i; j < i+k; j++ {
freqMap[nums[j]]++
}

var elements []ElementInfo
for val, freq := range freqMap {
elements = append(elements, ElementInfo{
Value: val,
Freq: freq,
})
}

sort.Slice(elements, func(a, b int) bool {
if elements[a].Freq != elements[b].Freq {
return elements[a].Freq > elements[b].Freq
}
return elements[a].Value > elements[b].Value
})

currentXSum := 0

limit := x
if len(elements) < x {
limit = len(elements)
}

for idx := 0; idx < limit; idx++ {
element := elements[idx]
currentXSum += element.Value * element.Freq
}

answer[i] = currentXSum
}

return answer
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
k, x int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 1, 2, 2, 3, 4, 2, 3}, 6, 2, []int{6, 10, 12}},
{"TestCase2", []int{3, 8, 7, 8, 7, 5}, 2, 2, []int{11, 15, 15, 15, 12}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k, c.x)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.inputs, c.k, c.x)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading