diff --git a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md index 06a8715f3..77fed5068 100755 --- a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md +++ b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md @@ -1,28 +1,46 @@ # [1664.Ways to Make a Fair Array][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`. You can choose **exactly one** index (**0-indexed**) and remove the element. Notice that the index of the elements may change after the removal. + +For example, if `nums = [6,1,7,4,1]`: + +- Choosing to remove index `1` results in `nums = [6,7,4,1]`. +- Choosing to remove index `2` results in `nums = [6,1,4,1]`. +- Choosing to remove index `4` results in `nums = [6,1,7,4]`. + +An array is **fair** if the sum of the odd-indexed values equals the sum of the even-indexed values. + +Return the **number** of indices that you could choose such that after the removal, `nums` is **fair**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,1,6,4] +Output: 1 +Explanation: +Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair. +Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair. +Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair. +Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair. +There is 1 index that you can remove to make nums fair. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Ways to Make a Fair Array -```go ``` +Input: nums = [1,1,1] +Output: 3 +Explanation: You can remove any index and the remaining array is fair. +``` + +**Example 3:** +``` +Input: nums = [1,2,3] +Output: 0 +Explanation: You cannot make a fair array after removing any index. +``` ## 结语 diff --git a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution.go b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution.go index d115ccf5e..2b84a6bbe 100644 --- a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution.go +++ b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + l := len(nums) + sum := make([][2]int, l) + if (l-1)&1 == 0 { + sum[l-1][0] += nums[l-1] + } else { + sum[l-1][1] += nums[l-1] + } + for i := l - 2; i >= 0; i-- { + sum[i] = sum[i+1] + if i&1 == 0 { + sum[i][0] += nums[i] + continue + } + sum[i][1] += nums[i] + } + odd, even := 0, 0 + ans := 0 + for i := range l - 1 { + if odd+sum[i+1][0] == even+sum[i+1][1] { + ans++ + } + if i&1 == 1 { + odd += nums[i] + continue + } + even += nums[i] + } + if odd == even { + ans++ + } + return ans } diff --git a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution_test.go b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution_test.go index 14ff50eb4..fb4f47a2c 100644 --- a/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution_test.go +++ b/leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/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{2, 1, 6, 4}, 1}, + {"TestCase2", []int{1, 1, 1}, 3}, + {"TestCase3", []int{1, 2, 3}, 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }