From f915f6aab8e02397f0517abe6196cfc52088ea8c Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 27 Feb 2025 09:21:38 +0800 Subject: [PATCH] Add solution and test-cases for problem 2606 --- .../README.md | 43 +++++++++++++++++++ .../Solution.go | 25 ++++++++++- .../Solution_test.go | 22 +++++----- 3 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/README.md diff --git a/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/README.md b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/README.md new file mode 100644 index 000000000..38a985a11 --- /dev/null +++ b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/README.md @@ -0,0 +1,43 @@ +# [2606.Find the Substring With Maximum Cost][title] + +## Description +You are given a string `s`, a string `chars` of **distinct** characters and an integer array `vals` of the same length as `chars`. + +The **cost of the substring** is the sum of the values of each character in the substring. The cost of an empty string is considered `0`. + +The **value of the character** is defined in the following way: + +- If the character is not in the string `chars`, then its value is its corresponding position **(1-indexed)** in the alphabet. + + - For example, the value of `'a'` is `1`, the value of `'b'` is `2`, and so on. The value of `'z'` is `26`. + +- Otherwise, assuming `i` is the index where the character occurs in the string `chars`, then its value is `vals[i]`. + +Return the maximum cost among all substrings of the string `s`. + +**Example 1:** + +``` +Input: s = "adaa", chars = "d", vals = [-1000] +Output: 2 +Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively. +The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2. +It can be proven that 2 is the maximum cost. +``` + +**Example 2:** + +``` +Input: s = "abc", chars = "abc", vals = [-1,-1,-1] +Output: 0 +Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively. +The substring with the maximum cost is the empty substring "" and its cost is 0. +It can be proven that 0 is the maximum cost. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/find-the-substring-with-maximum-cost/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution.go b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution.go index d115ccf5e..9d9f978d5 100755 --- a/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution.go +++ b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string, chars string, vals []int) int { + value := [26]int{} + for i := range 26 { + value[i] = i + 1 + } + for i, c := range chars { + value[c-'a'] = vals[i] + } + vv := make([]int, len(s)) + for i := range s { + vv[i] = value[s[i]-'a'] + } + + ans, minSum, sum := 0, 0, 0 + for i := 0; i < len(vv); i++ { + ans = max(ans, vv[i]) + sum += vv[i] + ans = max(ans, sum-minSum) + if sum < minSum { + minSum = sum + } + } + return ans } diff --git a/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution_test.go b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution_test.go index 14ff50eb4..56e6a9a2b 100755 --- a/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution_test.go +++ b/leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + s, chars string + vals []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "adaa", "d", []int{-1000}, 2}, + {"TestCase2", "abc", "abc", []int{-1, -1, -1}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s, c.chars, c.vals) 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.s, c.chars, c.vals) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }