Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 3255 #1034

Merged
merged 1 commit into from
Nov 17, 2024
Merged
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,50 @@
# [3255.Find the Power of K-Size Subarrays II][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 of integers `nums` of length `n` and a positive integer `k`.

The **power** of an array is defined as:

- Its **maximum** element if all of its elements are **consecutive** and **sorted** in **ascending** order.
- -1 otherwise.

You need to find the **power** of all subarrays of `nums` of size `k`.

Return an integer array `results` of size `n - k + 1`, where `results[i]` is the power of `nums[i..(i + k - 1)]`.

**Example 1:**

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

Output: [3,4,-1,-1,-1]

Explanation:

There are 5 subarrays of nums of size 3:

[1, 2, 3] with the maximum element 3.
[2, 3, 4] with the maximum element 4.
[3, 4, 3] whose elements are not consecutive.
[4, 3, 2] whose elements are not sorted.
[3, 2, 5] whose elements are not consecutive.
```

**Example 2:**

```
Input: nums = [2,2,2,2,2], k = 4

## 题意
> ...
Output: [-1,-1]
```

## 题解
**Example 2:**

### 思路1
> ...
Find the Power of K-Size Subarrays II
```go
```
Input: nums = [3,2,3,2,3,2], k = 2

Output: [-1,3,-1,3,-1]
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(nums []int, k int) []int {
l := len(nums)
ml := make([]int, l)
ml[0] = 1
for i := 1; i < l; i++ {
if nums[i]-nums[i-1] == 1 {
ml[i] = ml[i-1] + 1
continue
}
ml[i] = 1
}
ans := make([]int, l-k+1)
idx := 0
for i := k - 1; i < l; i, idx = i+1, idx+1 {
ans[idx] = -1
if ml[i] >= k {
ans[idx] = nums[i]
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 4, 3, 2, 5}, 3, []int{3, 4, -1, -1, -1}},
{"TestCase2", []int{2, 2, 2, 2, 2}, 4, []int{-1, -1}},
{"TestCase3", []int{3, 2, 3, 2, 3, 2}, 2, []int{-1, 3, -1, 3, -1}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k)
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",
c.expect, got, c.nums, c.k)
}
})
}
}

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

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