Skip to content

Commit cbde64d

Browse files
authored
Create 1787-make-the-xor-of-all-segments-equal-to-zero.js
1 parent 8ec9439 commit cbde64d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const L = 2 ** 10;
7+
const MAX = Number.MAX_SAFE_INTEGER;
8+
const mi = Math.min;
9+
const minChanges = (a, k) => {
10+
let n = a.length;
11+
let dp = Array(L).fill(MAX);
12+
dp[0] = 0;
13+
for (let i = 0; i < k; i++) {
14+
let tmp = Array(L).fill(0);
15+
let tot = 0;
16+
for (let j = i; j < n; j += k) {
17+
tmp[a[j]]++; // frequency count of starting points from each kth continuous subarray
18+
tot++; // total count of starting points from each kth continuous subarray
19+
}
20+
let ndp = Array(L).fill(0);
21+
let min = MAX;
22+
for (let j = 0; j < L; j++) {
23+
min = mi(min, dp[j]);
24+
}
25+
min += tot;
26+
ndp = ndp.map(x => x = min); // updated nested dp array with min value
27+
for (let j = 0; j < L; j++) {
28+
if (tmp[j] != 0) {
29+
for (let m = 0; m < L; m++) {
30+
ndp[m ^ j] = mi(ndp[m ^ j], dp[m] + tot - tmp[j]);
31+
}
32+
}
33+
}
34+
dp = ndp; // reset dp
35+
}
36+
return dp[0];
37+
};

0 commit comments

Comments
 (0)