Skip to content

Commit 802575a

Browse files
authored
Merge pull request #861 from 0xff-dev/2997
Add solution and test-cases for problem 2997
2 parents 3f931d5 + fc1ec9e commit 802575a

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2997.Minimum Number of Operations to Make Array XOR Equal to K][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 a **0-indexed** integer array `nums` and a positive integer `k`.
5+
6+
You can apply the following operation on the array **any** number of times:
7+
8+
- Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa.
9+
10+
Return the **minimum** number of operations required to make the bitwise `XOR` of all elements of the final array equal to `k`.
11+
12+
**Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number `(101)2` you can flip the fourth bit and obtain `(1101)2`.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [2,1,3,4], k = 1
18+
Output: 2
19+
Explanation: We can do the following operations:
20+
- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
21+
- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
22+
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
23+
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Minimum Number of Operations to Make Array XOR Equal to K
23-
```go
2428
```
25-
29+
Input: nums = [2,0,2,0], k = 0
30+
Output: 0
31+
Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
mask := 1
5+
ans := 0
6+
for shift := 0; shift < 32; shift++ {
7+
cur := mask << shift
8+
kbit := k & cur
9+
xor := 0
10+
for _, n := range nums {
11+
xor ^= (n & cur)
12+
}
13+
if xor != kbit {
14+
ans++
15+
}
16+
}
17+
return ans
518
}

leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)