Skip to content

Commit 9eedcd7

Browse files
authored
Merge pull request #1100 from 0xff-dev/1664
Add solution and test-cases for problem 1664
2 parents 4444859 + da41c5f commit 9eedcd7

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [1664.Ways to Make a Fair Array][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`. You can choose **exactly one** index (**0-indexed**) and remove the element. Notice that the index of the elements may change after the removal.
5+
6+
For example, if `nums = [6,1,7,4,1]`:
7+
8+
- Choosing to remove index `1` results in `nums = [6,7,4,1]`.
9+
- Choosing to remove index `2` results in `nums = [6,1,4,1]`.
10+
- Choosing to remove index `4` results in `nums = [6,1,7,4]`.
11+
12+
An array is **fair** if the sum of the odd-indexed values equals the sum of the even-indexed values.
13+
14+
Return the **number** of indices that you could choose such that after the removal, `nums` is **fair**.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [2,1,6,4]
20+
Output: 1
21+
Explanation:
22+
Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.
23+
Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair.
24+
Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.
25+
Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.
26+
There is 1 index that you can remove to make nums fair.
1327
```
1428

15-
## 题意
16-
> ...
29+
**Example 2:**
1730

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Ways to Make a Fair Array
23-
```go
2431
```
32+
Input: nums = [1,1,1]
33+
Output: 3
34+
Explanation: You can remove any index and the remaining array is fair.
35+
```
36+
37+
**Example 3:**
2538

39+
```
40+
Input: nums = [1,2,3]
41+
Output: 0
42+
Explanation: You cannot make a fair array after removing any index.
43+
```
2644

2745
## 结语
2846

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
l := len(nums)
5+
sum := make([][2]int, l)
6+
if (l-1)&1 == 0 {
7+
sum[l-1][0] += nums[l-1]
8+
} else {
9+
sum[l-1][1] += nums[l-1]
10+
}
11+
for i := l - 2; i >= 0; i-- {
12+
sum[i] = sum[i+1]
13+
if i&1 == 0 {
14+
sum[i][0] += nums[i]
15+
continue
16+
}
17+
sum[i][1] += nums[i]
18+
}
19+
odd, even := 0, 0
20+
ans := 0
21+
for i := range l - 1 {
22+
if odd+sum[i+1][0] == even+sum[i+1][1] {
23+
ans++
24+
}
25+
if i&1 == 1 {
26+
odd += nums[i]
27+
continue
28+
}
29+
even += nums[i]
30+
}
31+
if odd == even {
32+
ans++
33+
}
34+
return ans
535
}

leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution_test.go

+7-7
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{2, 1, 6, 4}, 1},
17+
{"TestCase2", []int{1, 1, 1}, 3},
18+
{"TestCase3", []int{1, 2, 3}, 0},
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)