Skip to content

Commit e2c4d36

Browse files
committed
feat: add solution 1787. Make the XOR of All Segments Equal to Zero
1 parent ce75f5f commit e2c4d36

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [1787. Make the XOR of All Segments Equal to Zero](https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given an array <code>nums</code>​​​ and an integer <code>k</code>​​​​​. The <font face="monospace">XOR</font> of a segment <code>[left, right]</code> where <code>left &lt;= right</code> is the <code>XOR</code> of all the elements with indices between <code>left</code> and <code>right</code>, inclusive: <code>nums[left] XOR nums[left+1] XOR ... XOR nums[right]</code>.</p>
6+
7+
<p>Return <em>the minimum number of elements to change in the array </em>such that the <code>XOR</code> of all segments of size <code>k</code>​​​​​​ is equal to zero.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> nums = [1,2,0,3,0], k = 1
13+
<strong>Output:</strong> 3
14+
<strong>Explanation: </strong>Modify the array from [<u><strong>1</strong></u>,<u><strong>2</strong></u>,0,<u><strong>3</strong></u>,0] to from [<u><strong>0</strong></u>,<u><strong>0</strong></u>,0,<u><strong>0</strong></u>,0].
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> nums = [3,4,5,2,1,7,3,4,7], k = 3
20+
<strong>Output:</strong> 3
21+
<strong>Explanation: </strong>Modify the array from [3,4,<strong><u>5</u></strong>,<strong><u>2</u></strong>,<strong><u>1</u></strong>,7,3,4,7] to [3,4,<strong><u>7</u></strong>,<strong><u>3</u></strong>,<strong><u>4</u></strong>,7,3,4,7].
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
26+
<pre><strong>Input:</strong> nums = [1,2,4,1,2,5,1,2,6], k = 3
27+
<strong>Output:</strong> 3
28+
<strong>Explanation: </strong>Modify the array from [1,2,<strong><u>4,</u></strong>1,2,<strong><u>5</u></strong>,1,2,<strong><u>6</u></strong>] to [1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>].</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= k &lt;= nums.length &lt;= 2000</code></li>
35+
<li><code>​​​​​​0 &lt;= nums[i] &lt; 2<sup>10</sup></code></li>
36+
</ul>
37+
</div>
38+
39+
<p>&nbsp;</p>
40+
41+
## Solutions
42+
43+
**Solution: `Dynamic Programming`**
44+
45+
- Time complexity: <em>O(n<sup>2</sup>)</em>
46+
- Space complexity: <em>O(nk)</em>
47+
48+
<p>&nbsp;</p>
49+
50+
### **JavaScript**
51+
52+
```js
53+
/**
54+
* @param {number[]} nums
55+
* @param {number} k
56+
* @return {number}
57+
*/
58+
const minChanges = function (nums, k) {
59+
const MAX_XOR = 1024;
60+
const n = nums.length;
61+
const counts = Array.from({ length: k }, () => new Map());
62+
const dp = Array.from({ length: k }, () => new Array(MAX_XOR).fill(n));
63+
64+
const getGroupCount = index => Math.floor(n / k) + (n % k > index ? 1 : 0);
65+
66+
for (let index = 0; index < n; index++) {
67+
const num = nums[index];
68+
const group = counts[index % k];
69+
const count = group.get(num) ?? 0;
70+
71+
group.set(num, count + 1);
72+
}
73+
74+
for (let xor = 0; xor < MAX_XOR; xor++) {
75+
const count = counts[k - 1].get(xor) ?? 0;
76+
77+
dp[k - 1][xor] = getGroupCount(k - 1) - count;
78+
}
79+
80+
for (let index = k - 2; index >= 0; index--) {
81+
const groupCount = getGroupCount(index);
82+
const nextMin = Math.min(...dp[index + 1]);
83+
84+
for (let xor = 0; xor < MAX_XOR; xor++) {
85+
dp[index][xor] = groupCount + nextMin;
86+
87+
for (const [num, count] of counts[index]) {
88+
const cost = groupCount - count;
89+
90+
dp[index][xor] = Math.min(dp[index][xor], dp[index + 1][xor ^ num] + cost);
91+
}
92+
}
93+
}
94+
95+
return dp[0][0];
96+
};
97+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minChanges = function (nums, k) {
7+
const MAX_XOR = 1024;
8+
const n = nums.length;
9+
const counts = Array.from({ length: k }, () => new Map());
10+
const dp = Array.from({ length: k }, () => new Array(MAX_XOR).fill(n));
11+
12+
const getGroupCount = index => Math.floor(n / k) + (n % k > index ? 1 : 0);
13+
14+
for (let index = 0; index < n; index++) {
15+
const num = nums[index];
16+
const group = counts[index % k];
17+
const count = group.get(num) ?? 0;
18+
19+
group.set(num, count + 1);
20+
}
21+
22+
for (let xor = 0; xor < MAX_XOR; xor++) {
23+
const count = counts[k - 1].get(xor) ?? 0;
24+
25+
dp[k - 1][xor] = getGroupCount(k - 1) - count;
26+
}
27+
28+
for (let index = k - 2; index >= 0; index--) {
29+
const groupCount = getGroupCount(index);
30+
const nextMin = Math.min(...dp[index + 1]);
31+
32+
for (let xor = 0; xor < MAX_XOR; xor++) {
33+
dp[index][xor] = groupCount + nextMin;
34+
35+
for (const [num, count] of counts[index]) {
36+
const cost = groupCount - count;
37+
38+
dp[index][xor] = Math.min(dp[index][xor], dp[index + 1][xor ^ num] + cost);
39+
}
40+
}
41+
}
42+
43+
return dp[0][0];
44+
};

0 commit comments

Comments
 (0)