-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #868 from 0xff-dev/1442
Add solution and test-cases for problem 1442
- Loading branch information
Showing
6 changed files
with
107 additions
and
49 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
leetcode/1401-1500/1422.Maximum-Score-After-Splitting-a-String/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# [1422.Maximum Score After Splitting a String][title] | ||
|
||
## Description | ||
Given a string `s` of zeros and ones, return the maximum score after splitting the string into two **non-empty** substrings (i.e. **left** substring and **right** substring). | ||
|
||
The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: s = "011101" | ||
Output: 5 | ||
Explanation: | ||
All possible ways of splitting s into two non-empty substrings are: | ||
left = "0" and right = "11101", score = 1 + 4 = 5 | ||
left = "01" and right = "1101", score = 1 + 3 = 4 | ||
left = "011" and right = "101", score = 1 + 2 = 3 | ||
left = "0111" and right = "01", score = 1 + 1 = 2 | ||
left = "01110" and right = "1", score = 2 + 1 = 3 | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: s = "00111" | ||
Output: 5 | ||
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5 | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: s = "1111" | ||
Output: 3 | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/maximum-score-after-splitting-a-string | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
20 changes: 18 additions & 2 deletions
20
leetcode/1401-1500/1422.Maximum-Score-After-Splitting-a-String/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(s string) int { | ||
l := len(s) | ||
sum := make([]int, l) | ||
sum[0] = int(s[0] - '0') | ||
|
||
for idx := 1; idx < l; idx++ { | ||
sum[idx] = int(s[idx]-'0') + sum[idx-1] | ||
} | ||
|
||
ans := 0 | ||
for idx := 0; idx < l-1; idx++ { | ||
right := sum[l-1] - sum[idx] | ||
left := idx + 1 - sum[idx] | ||
if r := left + right; r > ans { | ||
ans = r | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 16 additions & 21 deletions
37
...e/1401-1500/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 21 additions & 15 deletions
36
leetcode/1401-1500/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
package Solution | ||
|
||
func Solution(s string) int { | ||
l := len(s) | ||
sum := make([]int, l) | ||
sum[0] = int(s[0] - '0') | ||
|
||
for idx := 1; idx < l; idx++ { | ||
sum[idx] = int(s[idx]-'0') + sum[idx-1] | ||
} | ||
|
||
ans := 0 | ||
for idx := 0; idx < l-1; idx++ { | ||
right := sum[l-1] - sum[idx] | ||
left := idx + 1 - sum[idx] | ||
if r := left + right; r > ans { | ||
ans = r | ||
// max(n)=300, So O(n^3) is ok. | ||
func Solution(arr []int) int { | ||
ans, xor := 0, arr[0] | ||
l := len(arr) | ||
cache := make([]int, l) | ||
cache[0] = xor | ||
for k := 1; k < l; k++ { | ||
xor ^= arr[k] | ||
now := 0 | ||
for j := k; j > 0; j-- { | ||
now ^= arr[k] | ||
left := xor ^ now | ||
if left == now { | ||
ans++ | ||
} | ||
for i := j - 1; i > 0; i-- { | ||
if left^cache[i-1] == now { | ||
ans++ | ||
} | ||
} | ||
} | ||
cache[k] = xor | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters