Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1664 #1100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/1601-1700/1664.Ways-to-Make-a-Fair-Array/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading