diff --git a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/README.md b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/README.md index b6fd9e07f..e5aa62663 100755 --- a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/README.md +++ b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/README.md @@ -1,28 +1,55 @@ # [3432.Count Partitions with Even Sum Difference][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer array `nums` of length `n`. + +A **partition** is defined as an index `i` where `0 <= i < n - 1`, splitting the array into two **non-empty** subarrays such that: + +- Left subarray contains indices `[0, i]`. +- Right subarray contains indices `[i + 1, n - 1]`. + +Return the number of **partitions** where the **difference** between the **sum** of the left and right subarrays is **even**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [10,10,3,7,6] + +Output: 4 + +Explanation: + +The 4 partitions are: + +[10], [10, 3, 7, 6] with a sum difference of 10 - 26 = -16, which is even. +[10, 10], [3, 7, 6] with a sum difference of 20 - 16 = 4, which is even. +[10, 10, 3], [7, 6] with a sum difference of 23 - 13 = 10, which is even. +[10, 10, 3, 7], [6] with a sum difference of 30 - 6 = 24, which is even. ``` -## 题意 -> ... +**Example 2:** -## 题解 +``` +Input: nums = [1,2,2] + +Output: 0 + +Explanation: + +No partition results in an even sum difference. +``` + +**Example 3:** -### 思路1 -> ... -Count Partitions with Even Sum Difference -```go ``` +Input: nums = [2,4,6,8] + +Output: 3 +Explanation: + +All partitions result in an even sum difference. +``` ## 结语 diff --git a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution.go b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution.go index d115ccf5e..026d618ec 100644 --- a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution.go +++ b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + var ret, diff int + l := len(nums) + for i := 1; i < l; i++ { + nums[i] += nums[i-1] + } + // 1, 2, 3, 4 + for i := 0; i < l-1; i++ { + if diff = nums[l-1] - nums[i] - nums[i]; diff&1 == 0 { + ret++ + } + } + return ret } diff --git a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution_test.go b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution_test.go index 14ff50eb4..dfa8ea423 100644 --- a/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution_test.go +++ b/leetcode/3401-3500/3432.Count-Partitions-with-Even-Sum-Difference/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 10, 3, 7, 6}, 4}, + {"TestCase2", []int{1, 2, 2}, 0}, + {"TestCase3", []int{2, 4, 6, 8}, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }