Skip to content

Commit 1f6547d

Browse files
committed
feat: add solutions to lc problem: No.2606
No.2606.Find the Substring With Maximum Cost
1 parent 4868f8d commit 1f6547d

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

solution/2600-2699/2606.Find the Substring With Maximum Cost/README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:前缀和 + 维护前缀和的最小值**
64+
65+
我们根据题目描述,遍历字符串 $s$ 的每个字符 $c$,求出其对应的价值 $v$,然后更新当前的前缀和 $tot=tot+v$,那么以 $c$ 结尾的最大开销子字符串的开销为 $tot$ 减去前缀和的最小值 $mi$,即 $tot-mi$,我们更新答案 $ans=max(ans,tot-mi)$,并维护前缀和的最小值 $mi=min(mi,tot)$。
66+
67+
遍历结束后返回答案 $ans$ 即可。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度;而 $C$ 为字符集的大小,本题中 $C=26$。
70+
71+
**方法二:转化为最大子数组和问题**
72+
73+
我们可以将每个字符 $c$ 的价值 $v$ 看作是一个整数,那么题目实际上转化为求最大子数组和问题。
74+
75+
我们用变量 $f$ 维护以当前字符 $c$ 结尾的最大开销子字符串的开销,每次遍历到一个字符 $c$,更新 $f=max(f, 0) + v$,然后更新答案 $ans=max(ans,f)$ 即可。
76+
77+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度;而 $C$ 为字符集的大小,本题中 $C=26$。
78+
6379
<!-- tabs:start -->
6480

6581
### **Python3**
@@ -79,6 +95,18 @@ class Solution:
7995
return ans
8096
```
8197

98+
```python
99+
class Solution:
100+
def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:
101+
d = {c: i for i, c in enumerate(chars)}
102+
ans = f = 0
103+
for c in s:
104+
v = vals[d[c]] if c in d else ord(c) - ord('a') + 1
105+
f = max(f, 0) + v
106+
ans = max(ans, f)
107+
return ans
108+
```
109+
82110
### **Java**
83111

84112
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -106,6 +134,28 @@ class Solution {
106134
}
107135
```
108136

137+
```java
138+
class Solution {
139+
public int maximumCostSubstring(String s, String chars, int[] vals) {
140+
int[] d = new int[26];
141+
Arrays.fill(d, -1);
142+
int m = chars.length();
143+
for (int i = 0; i < m; ++i) {
144+
d[chars.charAt(i) - 'a'] = i;
145+
}
146+
int ans = 0, f = 0;
147+
int n = s.length();
148+
for (int i = 0; i < n; ++i) {
149+
int j = s.charAt(i) - 'a';
150+
int v = d[j] == -1 ? j + 1 : vals[d[j]];
151+
f = Math.max(f, 0) + v;
152+
ans = Math.max(ans, f);
153+
}
154+
return ans;
155+
}
156+
}
157+
```
158+
109159
### **C++**
110160

111161
```cpp
@@ -130,6 +180,27 @@ public:
130180
};
131181
```
132182
183+
```cpp
184+
class Solution {
185+
public:
186+
int maximumCostSubstring(string s, string chars, vector<int>& vals) {
187+
vector<int> d(26, -1);
188+
int m = chars.size();
189+
for (int i = 0; i < m; ++i) {
190+
d[chars[i] - 'a'] = i;
191+
}
192+
int ans = 0, f = 0;
193+
for (char& c : s) {
194+
int j = c - 'a';
195+
int v = d[j] == -1 ? j + 1 : vals[d[j]];
196+
f = max(f, 0) + v;
197+
ans = max(ans, f);
198+
}
199+
return ans;
200+
}
201+
};
202+
```
203+
133204
### **Go**
134205

135206
```go
@@ -170,6 +241,43 @@ func min(a, b int) int {
170241
}
171242
```
172243

244+
```go
245+
func maximumCostSubstring(s string, chars string, vals []int) (ans int) {
246+
d := [26]int{}
247+
for i := range d {
248+
d[i] = -1
249+
}
250+
for i, c := range chars {
251+
d[c-'a'] = i
252+
}
253+
f := 0
254+
for _, c := range s {
255+
j := int(c - 'a')
256+
v := j + 1
257+
if d[j] != -1 {
258+
v = vals[d[j]]
259+
}
260+
f = max(f, 0) + v
261+
ans = max(ans, f)
262+
}
263+
return
264+
}
265+
266+
func max(a, b int) int {
267+
if a > b {
268+
return a
269+
}
270+
return b
271+
}
272+
273+
func min(a, b int) int {
274+
if a < b {
275+
return a
276+
}
277+
return b
278+
}
279+
```
280+
173281
### **...**
174282

175283
```

solution/2600-2699/2606.Find the Substring With Maximum Cost/README_EN.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ class Solution:
7373
return ans
7474
```
7575

76+
```python
77+
class Solution:
78+
def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:
79+
d = {c: i for i, c in enumerate(chars)}
80+
ans = f = 0
81+
for c in s:
82+
v = vals[d[c]] if c in d else ord(c) - ord('a') + 1
83+
f = max(f, 0) + v
84+
ans = max(ans, f)
85+
return ans
86+
```
87+
7688
### **Java**
7789

7890
```java
@@ -98,6 +110,28 @@ class Solution {
98110
}
99111
```
100112

113+
```java
114+
class Solution {
115+
public int maximumCostSubstring(String s, String chars, int[] vals) {
116+
int[] d = new int[26];
117+
Arrays.fill(d, -1);
118+
int m = chars.length();
119+
for (int i = 0; i < m; ++i) {
120+
d[chars.charAt(i) - 'a'] = i;
121+
}
122+
int ans = 0, f = 0;
123+
int n = s.length();
124+
for (int i = 0; i < n; ++i) {
125+
int j = s.charAt(i) - 'a';
126+
int v = d[j] == -1 ? j + 1 : vals[d[j]];
127+
f = Math.max(f, 0) + v;
128+
ans = Math.max(ans, f);
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
101135
### **C++**
102136

103137
```cpp
@@ -122,6 +156,27 @@ public:
122156
};
123157
```
124158
159+
```cpp
160+
class Solution {
161+
public:
162+
int maximumCostSubstring(string s, string chars, vector<int>& vals) {
163+
vector<int> d(26, -1);
164+
int m = chars.size();
165+
for (int i = 0; i < m; ++i) {
166+
d[chars[i] - 'a'] = i;
167+
}
168+
int ans = 0, f = 0;
169+
for (char& c : s) {
170+
int j = c - 'a';
171+
int v = d[j] == -1 ? j + 1 : vals[d[j]];
172+
f = max(f, 0) + v;
173+
ans = max(ans, f);
174+
}
175+
return ans;
176+
}
177+
};
178+
```
179+
125180
### **Go**
126181

127182
```go
@@ -162,6 +217,43 @@ func min(a, b int) int {
162217
}
163218
```
164219

220+
```go
221+
func maximumCostSubstring(s string, chars string, vals []int) (ans int) {
222+
d := [26]int{}
223+
for i := range d {
224+
d[i] = -1
225+
}
226+
for i, c := range chars {
227+
d[c-'a'] = i
228+
}
229+
f := 0
230+
for _, c := range s {
231+
j := int(c - 'a')
232+
v := j + 1
233+
if d[j] != -1 {
234+
v = vals[d[j]]
235+
}
236+
f = max(f, 0) + v
237+
ans = max(ans, f)
238+
}
239+
return
240+
}
241+
242+
func max(a, b int) int {
243+
if a > b {
244+
return a
245+
}
246+
return b
247+
}
248+
249+
func min(a, b int) int {
250+
if a < b {
251+
return a
252+
}
253+
return b
254+
}
255+
```
256+
165257
### **...**
166258

167259
```

0 commit comments

Comments
 (0)