diff --git a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/README.md b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/README.md index 524b96794..d3754ac08 100755 --- a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/README.md +++ b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/README.md @@ -1,28 +1,35 @@ # [2997.Minimum Number of Operations to Make Array XOR Equal to K][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 a **0-indexed** integer array `nums` and a positive integer `k`. + +You can apply the following operation on the array **any** number of times: + +- 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. + +Return the **minimum** number of operations required to make the bitwise `XOR` of all elements of the final array equal to `k`. + +**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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,1,3,4], k = 1 +Output: 2 +Explanation: We can do the following operations: +- 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]. +- 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]. +The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. +It can be shown that we cannot make the XOR equal to k in less than 2 operations. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Minimum Number of Operations to Make Array XOR Equal to K -```go ``` - +Input: nums = [2,0,2,0], k = 0 +Output: 0 +Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. +``` ## 结语 diff --git a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution.go b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution.go index d115ccf5e..98cfc7147 100644 --- a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution.go +++ b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + mask := 1 + ans := 0 + for shift := 0; shift < 32; shift++ { + cur := mask << shift + kbit := k & cur + xor := 0 + for _, n := range nums { + xor ^= (n & cur) + } + if xor != kbit { + ans++ + } + } + return ans } diff --git a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution_test.go b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution_test.go index 14ff50eb4..28f9adfe1 100644 --- a/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution_test.go +++ b/leetcode/2901-3000/2997.Minimum-Number-of-Operations-to-Make-Array-XOR-Equal-to-K/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 1, 3, 4}, 1, 2}, + {"TestCase2", []int{2, 0, 2, 0}, 0, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }