Skip to content

Commit

Permalink
Merge pull request #667 from 0xff-dev/1793
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1793
  • Loading branch information
6boris authored Oct 28, 2023
2 parents a6b668a + ab1cefa commit 90aadbf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
27 changes: 13 additions & 14 deletions leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [1793.Maximum Score of a Good Subarray][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 (**0-indexed**) and an integer `k`.

The **score** of a subarray `(i, j)` is defined as `min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)`. A **good** subarray is a subarray where `i <= k <= j`.

Return the maximum possible **score** of a **good** subarray.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,4,3,7,4,5], k = 3
Output: 15
Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Maximum Score of a Good Subarray
```go
```

Input: nums = [5,5,4,5,4,1,1,1], k = 0
Output: 20
Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(nums []int, k int) int {
l := len(nums)
minArr := make([]int, l)
minArr[k] = nums[k]
for i := k - 1; i >= 0; i-- {
if nums[i] > minArr[i+1] {
minArr[i] = minArr[i+1]
} else {
minArr[i] = nums[i]
}
}
for j := k + 1; j < l; j++ {
if nums[j] > minArr[j-1] {
minArr[j] = minArr[j-1]
} else {
minArr[j] = nums[j]
}
}

ans := -1
for i := 0; i <= k; i++ {
if i > 0 && minArr[i] == minArr[i-1] {
continue
}
for j := l - 1; j >= k; j-- {
if j < k-1 && minArr[j] == minArr[j+1] {
continue
}
a := minArr[i]
b := minArr[j]
if b < a {
a = b
}
if r := a * (j - i + 1); ans == -1 || r > ans {
ans = r
}
}
}
return ans
}
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 int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 4, 3, 7, 4, 5}, 3, 15},
{"TestCase2", []int{5, 5, 4, 5, 4, 1, 1, 1}, 0, 20},
}

// 开始测试
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)
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.inputs, c.k)
}
})
}
}

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

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

0 comments on commit 90aadbf

Please sign in to comment.