diff --git a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/README.md b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/README.md index 54fca1c71..1a2ccffbe 100755 --- a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/README.md +++ b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution.go b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution.go index d115ccf5e..1d82d59ca 100644 --- a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution.go +++ b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution.go @@ -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 } diff --git a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution_test.go b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution_test.go index 14ff50eb4..c0031c765 100644 --- a/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution_test.go +++ b/leetcode/1701-1800/1793.Maximum-Score-of-a-Good-Subarray/Solution_test.go @@ -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() { }