Skip to content

feat: add solutions to lc problems: No.1422,1424,1425 #3427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 63 additions & 174 deletions solution/1400-1499/1422.Maximum Score After Splitting a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ tags:
<p><strong>示例 1:</strong></p>

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

Expand Down Expand Up @@ -66,7 +66,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:计数

我们用两个变量 $l$ 和 $r$ 分别记录左子字符串中 $0$ 的数量和右子字符串中 $1$ 的数量。初始时 $l = 0$,而 $r$ 则等于字符串 $s$ 中 $1$ 的数量。

遍历字符串 $s$ 的前 $n - 1$ 个字符,对于每一个位置 $i$,如果 $s[i] = 0$,则 $l$ 自增 $1$,否则 $r$ 自减 $1$。然后我们更新答案为 $l + r$ 的最大值。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -75,28 +81,32 @@ tags:
```python
class Solution:
def maxScore(self, s: str) -> int:
return max(s[:i].count('0') + s[i:].count('1') for i in range(1, len(s)))
l, r = 0, s.count("1")
ans = 0
for x in s[:-1]:
l += int(x) ^ 1
r -= int(x)
ans = max(ans, l + r)
return ans
```

#### Java

```java
class Solution {
public int maxScore(String s) {
int ans = 0;
for (int i = 1; i < s.length(); ++i) {
int t = 0;
for (int j = 0; j < i; ++j) {
if (s.charAt(j) == '0') {
++t;
}
}
for (int j = i; j < s.length(); ++j) {
if (s.charAt(j) == '1') {
++t;
}
int l = 0, r = 0;
int n = s.length();
for (int i = 0; i < n; ++i) {
if (s.charAt(i) == '1') {
++r;
}
ans = Math.max(ans, t);
}
int ans = 0;
for (int i = 0; i < n - 1; ++i) {
l += (s.charAt(i) - '0') ^ 1;
r -= s.charAt(i) - '0';
ans = Math.max(ans, l + r);
}
return ans;
}
Expand All @@ -109,16 +119,12 @@ class Solution {
class Solution {
public:
int maxScore(string s) {
int l = 0, r = count(s.begin(), s.end(), '1');
int ans = 0;
for (int i = 1, n = s.size(); i < n; ++i) {
int t = 0;
for (int j = 0; j < i; ++j) {
t += s[j] == '0';
}
for (int j = i; j < n; ++j) {
t += s[j] == '1';
}
ans = max(ans, t);
for (int i = 0; i < s.size() - 1; ++i) {
l += (s[i] - '0') ^ 1;
r -= s[i] - '0';
ans = max(ans, l + r);
}
return ans;
}
Expand All @@ -128,51 +134,38 @@ public:
#### Go

```go
func maxScore(s string) int {
ans := 0
for i, n := 1, len(s); i < n; i++ {
t := 0
for j := 0; j < i; j++ {
if s[j] == '0' {
t++
}
}
for j := i; j < n; j++ {
if s[j] == '1' {
t++
}
func maxScore(s string) (ans int) {
l, r := 0, strings.Count(s, "1")
for _, c := range s[:len(s)-1] {
if c == '0' {
l++
} else {
r--
}
ans = max(ans, t)
ans = max(ans, l+r)
}
return ans
return
}
```

#### TypeScript

```ts
function maxScore(s: string): number {
const n = s.length;
let res = 0;
let score = 0;
if (s[0] === '0') {
score++;
let [l, r] = [0, 0];
for (const c of s) {
r += c === '1' ? 1 : 0;
}
for (let i = 1; i < n; i++) {
if (s[i] === '1') {
score++;
}
}
res = Math.max(res, score);
for (let i = 1; i < n - 1; i++) {
let ans = 0;
for (let i = 0; i < s.length - 1; ++i) {
if (s[i] === '0') {
score++;
} else if (s[i] === '1') {
score--;
++l;
} else {
--r;
}
res = Math.max(res, score);
ans = Math.max(ans, l + r);
}
return res;
return ans;
}
```

Expand All @@ -181,121 +174,17 @@ function maxScore(s: string): number {
```rust
impl Solution {
pub fn max_score(s: String) -> i32 {
let n = s.len();
let mut res = 0;
let mut score = 0;
let bs = s.as_bytes();
if bs[0] == b'0' {
score += 1;
}
for i in 1..n {
if bs[i] == b'1' {
score += 1;
}
}
res = res.max(score);
for i in 1..n - 1 {
if bs[i] == b'0' {
score += 1;
} else if bs[i] == b'1' {
score -= 1;
}
res = res.max(score);
}
res
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxScore(self, s: str) -> int:
ans = t = (s[0] == '0') + s[1:].count('1')
for i in range(1, len(s) - 1):
t += 1 if s[i] == '0' else -1
ans = max(ans, t)
return ans
```

#### Java

```java
class Solution {
public int maxScore(String s) {
int t = 0;
if (s.charAt(0) == '0') {
t++;
}
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == '1') {
t++;
}
}
int ans = t;
for (int i = 1; i < s.length() - 1; ++i) {
t += s.charAt(i) == '0' ? 1 : -1;
ans = Math.max(ans, t);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxScore(string s) {
int t = 0;
if (s[0] == '0') ++t;
for (int i = 1; i < s.size(); ++i) t += s[i] == '1';
int ans = t;
for (int i = 1; i < s.size() - 1; ++i) {
t += s[i] == '0' ? 1 : -1;
ans = max(ans, t);
let mut l = 0;
let mut r = s.bytes().filter(|&b| b == b'1').count() as i32;
let mut ans = 0;
let cs = s.as_bytes();
for i in 0..s.len() - 1 {
l += ((cs[i] - b'0') ^ 1) as i32;
r -= (cs[i] - b'0') as i32;
ans = ans.max(l + r);
}
return ans;
ans
}
};
```

#### Go

```go
func maxScore(s string) int {
t := 0
if s[0] == '0' {
t++
}
n := len(s)
for i := 1; i < n; i++ {
if s[i] == '1' {
t++
}
}
ans := t
for i := 1; i < n-1; i++ {
if s[i] == '0' {
t++
} else {
t--
}
ans = max(ans, t)
}
return ans
}
```

Expand Down
Loading
Loading