Skip to content

Commit 2a0b50d

Browse files
add 1422
1 parent 9a2debe commit 2a0b50d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1422.java) | |Easy|String|
1112
|1418|[Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1418.java) | |Medium|HashTable|
1213
|1417|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1417.java) | |Easy|String|
1314
|1415|[The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1415.java) | |Medium|Backtracking|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1422 {
4+
public static class Solution1 {
5+
public int maxScore(String s) {
6+
int zeroes = s.charAt(0) == '0' ? 1 : 0;
7+
int ones = 0;
8+
for (int i = 1; i < s.length(); i++) {
9+
if (s.charAt(i) == '1') {
10+
ones++;
11+
}
12+
}
13+
int maxScore = zeroes + ones;
14+
for (int i = 1; i < s.length() - 1; i++) {
15+
if (s.charAt(i) == '0') {
16+
zeroes++;
17+
} else {
18+
ones--;
19+
}
20+
maxScore = Math.max(maxScore, zeroes + ones);
21+
}
22+
return maxScore;
23+
}
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1422;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1422Test {
10+
private static _1422.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1422.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.maxScore("011101"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.maxScore("00111"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)