Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b973e2

Browse files
feat: add solutions to lc problems: No.1422,1424,1425 (doocs#3427)
* No.1424.Diagonal Traverse II * No.1425.Constrained Subsequence Sum
1 parent 074b513 commit 1b973e2

File tree

22 files changed

+921
-605
lines changed

22 files changed

+921
-605
lines changed

‎solution/1400-1499/1422.Maximum Score After Splitting a String/README.md‎

Lines changed: 63 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ tags:
2828
<p><strong>示例 1:</strong></p>
2929

3030
<pre><strong>输入:</strong>s = &quot;011101&quot;
31-
<strong>输出:</strong>5
31+
<strong>输出:</strong>5
3232
<strong>解释:</strong>
3333
将字符串 s 划分为两个非空子字符串的可行方案有:
34-
左子字符串 = &quot;0&quot; 且 右子字符串 = &quot;11101&quot;,得分 = 1 + 4 = 5
35-
左子字符串 = &quot;01&quot; 且 右子字符串 = &quot;1101&quot;,得分 = 1 + 3 = 4
36-
左子字符串 = &quot;011&quot; 且 右子字符串 = &quot;101&quot;,得分 = 1 + 2 = 3
37-
左子字符串 = &quot;0111&quot; 且 右子字符串 = &quot;01&quot;,得分 = 1 + 1 = 2
34+
左子字符串 = &quot;0&quot; 且 右子字符串 = &quot;11101&quot;,得分 = 1 + 4 = 5
35+
左子字符串 = &quot;01&quot; 且 右子字符串 = &quot;1101&quot;,得分 = 1 + 3 = 4
36+
左子字符串 = &quot;011&quot; 且 右子字符串 = &quot;101&quot;,得分 = 1 + 2 = 3
37+
左子字符串 = &quot;0111&quot; 且 右子字符串 = &quot;01&quot;,得分 = 1 + 1 = 2
3838
左子字符串 = &quot;01110&quot; 且 右子字符串 = &quot;1&quot;,得分 = 2 + 1 = 3
3939
</pre>
4040

@@ -66,7 +66,13 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### 方法一
69+
### 方法一:计数
70+
71+
我们用两个变量 $l$ 和 $r$ 分别记录左子字符串中 0ドル$ 的数量和右子字符串中 1ドル$ 的数量。初始时 $l = 0,ドル而 $r$ 则等于字符串 $s$ 中 1ドル$ 的数量。
72+
73+
遍历字符串 $s$ 的前 $n - 1$ 个字符,对于每一个位置 $i,ドル如果 $s[i] = 0,ドル则 $l$ 自增 1ドル,ドル否则 $r$ 自减 1ドル$。然后我们更新答案为 $l + r$ 的最大值。
74+
75+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
7076

7177
<!-- tabs:start -->
7278

@@ -75,28 +81,32 @@ tags:
7581
```python
7682
class Solution:
7783
def maxScore(self, s: str) -> int:
78-
return max(s[:i].count('0') + s[i:].count('1') for i in range(1, len(s)))
84+
l, r = 0, s.count("1")
85+
ans = 0
86+
for x in s[:-1]:
87+
l += int(x) ^ 1
88+
r -= int(x)
89+
ans = max(ans, l + r)
90+
return ans
7991
```
8092

8193
#### Java
8294

8395
```java
8496
class Solution {
8597
public int maxScore(String s) {
86-
int ans = 0;
87-
for (int i = 1; i < s.length(); ++i) {
88-
int t = 0;
89-
for (int j = 0; j < i; ++j) {
90-
if (s.charAt(j) == '0') {
91-
++t;
92-
}
93-
}
94-
for (int j = i; j < s.length(); ++j) {
95-
if (s.charAt(j) == '1') {
96-
++t;
97-
}
98+
int l = 0, r = 0;
99+
int n = s.length();
100+
for (int i = 0; i < n; ++i) {
101+
if (s.charAt(i) == '1') {
102+
++r;
98103
}
99-
ans = Math.max(ans, t);
104+
}
105+
int ans = 0;
106+
for (int i = 0; i < n - 1; ++i) {
107+
l += (s.charAt(i) - '0') ^ 1;
108+
r -= s.charAt(i) - '0';
109+
ans = Math.max(ans, l + r);
100110
}
101111
return ans;
102112
}
@@ -109,16 +119,12 @@ class Solution {
109119
class Solution {
110120
public:
111121
int maxScore(string s) {
122+
int l = 0, r = count(s.begin(), s.end(), '1');
112123
int ans = 0;
113-
for (int i = 1, n = s.size(); i < n; ++i) {
114-
int t = 0;
115-
for (int j = 0; j < i; ++j) {
116-
t += s[j] == '0';
117-
}
118-
for (int j = i; j < n; ++j) {
119-
t += s[j] == '1';
120-
}
121-
ans = max(ans, t);
124+
for (int i = 0; i < s.size() - 1; ++i) {
125+
l += (s[i] - '0') ^ 1;
126+
r -= s[i] - '0';
127+
ans = max(ans, l + r);
122128
}
123129
return ans;
124130
}
@@ -128,51 +134,38 @@ public:
128134
#### Go
129135
130136
```go
131-
func maxScore(s string) int {
132-
ans := 0
133-
for i, n := 1, len(s); i < n; i++ {
134-
t := 0
135-
for j := 0; j < i; j++ {
136-
if s[j] == '0' {
137-
t++
138-
}
139-
}
140-
for j := i; j < n; j++ {
141-
if s[j] == '1' {
142-
t++
143-
}
137+
func maxScore(s string) (ans int) {
138+
l, r := 0, strings.Count(s, "1")
139+
for _, c := range s[:len(s)-1] {
140+
if c == '0' {
141+
l++
142+
} else {
143+
r--
144144
}
145-
ans = max(ans, t)
145+
ans = max(ans, l+r)
146146
}
147-
return ans
147+
return
148148
}
149149
```
150150

151151
#### TypeScript
152152

153153
```ts
154154
function maxScore(s: string): number {
155-
const n = s.length;
156-
let res = 0;
157-
let score = 0;
158-
if (s[0] === '0') {
159-
score++;
155+
let [l, r] = [0, 0];
156+
for (const c of s) {
157+
r += c === '1' ? 1 : 0;
160158
}
161-
for (let i = 1; i < n; i++) {
162-
if (s[i] === '1') {
163-
score++;
164-
}
165-
}
166-
res = Math.max(res, score);
167-
for (let i = 1; i < n - 1; i++) {
159+
let ans = 0;
160+
for (let i = 0; i < s.length - 1; ++i) {
168161
if (s[i] === '0') {
169-
score++;
170-
} else if (s[i] ==='1') {
171-
score--;
162+
++l;
163+
} else {
164+
--r;
172165
}
173-
res = Math.max(res, score);
166+
ans = Math.max(ans, l+r);
174167
}
175-
return res;
168+
return ans;
176169
}
177170
```
178171

@@ -181,121 +174,17 @@ function maxScore(s: string): number {
181174
```rust
182175
impl Solution {
183176
pub fn max_score(s: String) -> i32 {
184-
let n = s.len();
185-
let mut res = 0;
186-
let mut score = 0;
187-
let bs = s.as_bytes();
188-
if bs[0] == b'0' {
189-
score += 1;
190-
}
191-
for i in 1..n {
192-
if bs[i] == b'1' {
193-
score += 1;
194-
}
195-
}
196-
res = res.max(score);
197-
for i in 1..n - 1 {
198-
if bs[i] == b'0' {
199-
score += 1;
200-
} else if bs[i] == b'1' {
201-
score -= 1;
202-
}
203-
res = res.max(score);
204-
}
205-
res
206-
}
207-
}
208-
```
209-
210-
<!-- tabs:end -->
211-
212-
<!-- solution:end -->
213-
214-
<!-- solution:start -->
215-
216-
### 方法二
217-
218-
<!-- tabs:start -->
219-
220-
#### Python3
221-
222-
```python
223-
class Solution:
224-
def maxScore(self, s: str) -> int:
225-
ans = t = (s[0] == '0') + s[1:].count('1')
226-
for i in range(1, len(s) - 1):
227-
t += 1 if s[i] == '0' else -1
228-
ans = max(ans, t)
229-
return ans
230-
```
231-
232-
#### Java
233-
234-
```java
235-
class Solution {
236-
public int maxScore(String s) {
237-
int t = 0;
238-
if (s.charAt(0) == '0') {
239-
t++;
240-
}
241-
for (int i = 1; i < s.length(); ++i) {
242-
if (s.charAt(i) == '1') {
243-
t++;
244-
}
245-
}
246-
int ans = t;
247-
for (int i = 1; i < s.length() - 1; ++i) {
248-
t += s.charAt(i) == '0' ? 1 : -1;
249-
ans = Math.max(ans, t);
250-
}
251-
return ans;
252-
}
253-
}
254-
```
255-
256-
#### C++
257-
258-
```cpp
259-
class Solution {
260-
public:
261-
int maxScore(string s) {
262-
int t = 0;
263-
if (s[0] == '0') ++t;
264-
for (int i = 1; i < s.size(); ++i) t += s[i] == '1';
265-
int ans = t;
266-
for (int i = 1; i < s.size() - 1; ++i) {
267-
t += s[i] == '0' ? 1 : -1;
268-
ans = max(ans, t);
177+
let mut l = 0;
178+
let mut r = s.bytes().filter(|&b| b == b'1').count() as i32;
179+
let mut ans = 0;
180+
let cs = s.as_bytes();
181+
for i in 0..s.len() - 1 {
182+
l += ((cs[i] - b'0') ^ 1) as i32;
183+
r -= (cs[i] - b'0') as i32;
184+
ans = ans.max(l + r);
269185
}
270-
return ans;
186+
ans
271187
}
272-
};
273-
```
274-
275-
#### Go
276-
277-
```go
278-
func maxScore(s string) int {
279-
t := 0
280-
if s[0] == '0' {
281-
t++
282-
}
283-
n := len(s)
284-
for i := 1; i < n; i++ {
285-
if s[i] == '1' {
286-
t++
287-
}
288-
}
289-
ans := t
290-
for i := 1; i < n-1; i++ {
291-
if s[i] == '0' {
292-
t++
293-
} else {
294-
t--
295-
}
296-
ans = max(ans, t)
297-
}
298-
return ans
299188
}
300189
```
301190

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /