Skip to content

Commit ec1ae07

Browse files
authored
Merge pull request #1134 from 0xff-dev/2606
Add solution and test-cases for problem 2606
2 parents 079089a + f915f6a commit ec1ae07

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [2606.Find the Substring With Maximum Cost][title]
2+
3+
## Description
4+
You are given a string `s`, a string `chars` of **distinct** characters and an integer array `vals` of the same length as `chars`.
5+
6+
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`.
7+
8+
The **value of the character** is defined in the following way:
9+
10+
- If the character is not in the string `chars`, then its value is its corresponding position **(1-indexed)** in the alphabet.
11+
12+
- For example, the value of `'a'` is `1`, the value of `'b'` is `2`, and so on. The value of `'z'` is `26`.
13+
14+
- Otherwise, assuming `i` is the index where the character occurs in the string `chars`, then its value is `vals[i]`.
15+
16+
Return the maximum cost among all substrings of the string `s`.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: s = "adaa", chars = "d", vals = [-1000]
22+
Output: 2
23+
Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively.
24+
The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2.
25+
It can be proven that 2 is the maximum cost.
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: s = "abc", chars = "abc", vals = [-1,-1,-1]
32+
Output: 0
33+
Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively.
34+
The substring with the maximum cost is the empty substring "" and its cost is 0.
35+
It can be proven that 0 is the maximum cost.
36+
```
37+
38+
## 结语
39+
40+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
41+
42+
[title]: https://leetcode.com/problems/find-the-substring-with-maximum-cost/
43+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, chars string, vals []int) int {
4+
value := [26]int{}
5+
for i := range 26 {
6+
value[i] = i + 1
7+
}
8+
for i, c := range chars {
9+
value[c-'a'] = vals[i]
10+
}
11+
vv := make([]int, len(s))
12+
for i := range s {
13+
vv[i] = value[s[i]-'a']
14+
}
15+
16+
ans, minSum, sum := 0, 0, 0
17+
for i := 0; i < len(vv); i++ {
18+
ans = max(ans, vv[i])
19+
sum += vv[i]
20+
ans = max(ans, sum-minSum)
21+
if sum < minSum {
22+
minSum = sum
23+
}
24+
}
25+
return ans
526
}

leetcode/2601-2700/2606.Find-the-Substring-With-Maximum-Cost/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
s, chars string
14+
vals []int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "adaa", "d", []int{-1000}, 2},
18+
{"TestCase2", "abc", "abc", []int{-1, -1, -1}, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.s, c.chars, c.vals)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.s, c.chars, c.vals)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)