Skip to content

Commit 04950df

Browse files
authored
Merge pull request #1387 from 0xff-dev/3432
Add solution and test-cases for problem 3432
2 parents 7bdd75a + 48b67ca commit 04950df

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [3432.Count Partitions with Even Sum Difference][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` of length `n`.
5+
6+
A **partition** is defined as an index `i` where `0 <= i < n - 1`, splitting the array into two **non-empty** subarrays such that:
7+
8+
- Left subarray contains indices `[0, i]`.
9+
- Right subarray contains indices `[i + 1, n - 1]`.
10+
11+
Return the number of **partitions** where the **difference** between the **sum** of the left and right subarrays is **even**.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [10,10,3,7,6]
17+
18+
Output: 4
19+
20+
Explanation:
21+
22+
The 4 partitions are:
23+
24+
[10], [10, 3, 7, 6] with a sum difference of 10 - 26 = -16, which is even.
25+
[10, 10], [3, 7, 6] with a sum difference of 20 - 16 = 4, which is even.
26+
[10, 10, 3], [7, 6] with a sum difference of 23 - 13 = 10, which is even.
27+
[10, 10, 3, 7], [6] with a sum difference of 30 - 6 = 24, which is even.
1328
```
1429

15-
## 题意
16-
> ...
30+
**Example 2:**
1731

18-
## 题解
32+
```
33+
Input: nums = [1,2,2]
34+
35+
Output: 0
36+
37+
Explanation:
38+
39+
No partition results in an even sum difference.
40+
```
41+
42+
**Example 3:**
1943

20-
### 思路1
21-
> ...
22-
Count Partitions with Even Sum Difference
23-
```go
2444
```
45+
Input: nums = [2,4,6,8]
46+
47+
Output: 3
2548
49+
Explanation:
50+
51+
All partitions result in an even sum difference.
52+
```
2653

2754
## 结语
2855

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
var ret, diff int
5+
l := len(nums)
6+
for i := 1; i < l; i++ {
7+
nums[i] += nums[i-1]
8+
}
9+
// 1, 2, 3, 4
10+
for i := 0; i < l-1; i++ {
11+
if diff = nums[l-1] - nums[i] - nums[i]; diff&1 == 0 {
12+
ret++
13+
}
14+
}
15+
return ret
516
}

leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{10, 10, 3, 7, 6}, 4},
17+
{"TestCase2", []int{1, 2, 2}, 0},
18+
{"TestCase3", []int{2, 4, 6, 8}, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)