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 c0a0c62

Browse files
author
Shuo
authored
Merge pull request #817 from openset/develop
A: Maximum Score After Splitting a String
2 parents 59e29e3 + 4bd0377 commit c0a0c62

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem1422
2+
3+
import "strings"
4+
5+
func maxScore(s string) int {
6+
ans := 0
7+
n0 := 0
8+
n1 := strings.Count(s, "1")
9+
for _, v := range s[:len(s)-1] {
10+
if v == '1' {
11+
n1--
12+
} else {
13+
n0++
14+
}
15+
if ans < n0+n1 {
16+
ans = n0 + n1
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem1422
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in string
7+
want int
8+
}
9+
10+
func TestMaxScore(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: "011101",
14+
want: 5,
15+
},
16+
{
17+
in: "00111",
18+
want: 5,
19+
},
20+
{
21+
in: "1111",
22+
want: 3,
23+
},
24+
{
25+
in: "000000",
26+
want: 5,
27+
},
28+
}
29+
for _, tt := range tests {
30+
got := maxScore(tt.in)
31+
if got != tt.want {
32+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

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