Skip to content

Commit 4bd0377

Browse files
author
Shuo
committed
A: Maximum Score After Splitting a String
1 parent 177c833 commit 4bd0377

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
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+
}
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)