forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeTheXorOfAllSegmentsEqualToZero.cpp
114 lines (94 loc) · 3.64 KB
/
MakeTheXorOfAllSegmentsEqualToZero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Source : https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero/
// Author : Hao Chen
// Date : 2021-03-20
/*****************************************************************************************************
*
* You are given an array nums and an integer k. The XOR of a segment [left, right] where left
* <= right is the XOR of all the elements with indices between left and right, inclusive: nums[left]
* XOR nums[left+1] XOR ... XOR nums[right].
*
* Return the minimum number of elements to change in the array such that the XOR of all segments of
* size k is equal to zero.
*
* Example 1:
*
* Input: nums = [1,2,0,3,0], k = 1
* Output: 3
* Explanation: Modify the array from [1,2,0,3,0] to from [0,0,0,0,0].
*
* Example 2:
*
* Input: nums = [3,4,5,2,1,7,3,4,7], k = 3
* Output: 3
* Explanation: Modify the array from [3,4,5,2,1,7,3,4,7] to [3,4,7,3,4,7,3,4,7].
*
* Example 3:
*
* Input: nums = [1,2,4,1,2,5,1,2,6], k = 3
* Output: 3
* Explanation: Modify the array from [1,2,4,1,2,5,1,2,6] to [1,2,3,1,2,3,1,2,3].
*
* Constraints:
*
* 1 <= k <= nums.length <= 2000
* 0 <= nums[i] < 210
******************************************************************************************************/
/*
First K length subarray:
a1^a2^a3^.....^ak=0 ;
Second K length subarray :
a2^a3^a4^....^ak+1=0 ;
On combining both :
a1^(a2^a3^....^ak)^(a2^a3^....^ak)^a[k+1] =0
a1^a[k+1]=0
Therefore it is easy to see that for any i and j :
a[i%k]^a[j%k] = 0 ;
-> a[i%k]=a[j%k] ;
Hence, a[1] = a[k+1] = a[2k+1] = ...
a[2] = a[k+2] = a[2k+2] = ...
a[3] = a[k+3] = a[2k+3] = ...
.
.
.
a[k] = a[2k] = a[3k] = ...
So we just need to obtain the first k length subarray and the rest would be determined by it.
*/
class Solution {
public:
int minChanges(vector<int>& nums, int k) {
const int max_value = 1024; //0 <= nums[i] < 2^10
int n = nums.size();
//freq[i][v] means frequency of the number `v` at `i` position in [0, k-1];
vector<vector<int>> freq(k, vector<int>(max_value, 0));
for (int i=0; i<n; i++) {
freq[i%k][nums[i]]++;
}
//dp[i][v] means minimum number of changes in first i elements such that the xor value is `v`
vector<vector<int>> dp(k, vector<int>(max_value, n+1));
//initailization
int minChanges = n + 1;
for (int v = 0; v < max_value; v++) {
int cntOfPos = n / k + (((n % k) > 0) ? 1 : 0);
dp[0][v] = cntOfPos - freq[0][v];
minChanges = min(minChanges, dp[0][v]);
}
for (int i=1; i<k; i++) {
// how many i indices exist in the array
int cntOfPos = n / k + (((n % k) > i) ? 1 : 0);
//track minimum changes
int m = n + 1;
//for all of possible values
for (int v = 0; v < max_value; v++) {
for (int j = i; j < n; j += k) {
int x = v ^ nums[j];
dp[i][v] = min( dp[i][v], dp[i-1][x] + cntOfPos - freq[i][nums[j]]);
}
//for all of numbers don't occur at index i
dp[i][v] = min(dp[i][v], minChanges + cntOfPos);
m = min(m, dp[i][v]);
}
minChanges = m;
}
return dp[k-1][0];
}
};