forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-the-xor-of-all-segments-equal-to-zero.cpp
43 lines (40 loc) · 1.42 KB
/
make-the-xor-of-all-segments-equal-to-zero.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
// Time: O(n + k * m), m is the max number of nums
// Space: O(min(k * m, n))
class Solution {
public:
int minChanges(vector<int>& nums, int k) {
vector<unordered_map<int, int>> cnts(k);
for (int i = 0; i < size(nums); ++i) {
++cnts[i % k][nums[i]];
}
return min(one_are_not_from_nums(nums, cnts), all_are_from_nums(nums, cnts));
}
private:
int all_are_from_nums(const vector<int>& nums, const vector<unordered_map<int, int>>& cnts) {
unordered_map<int, int> dp{{0, 0}};
for (const auto& cnt : cnts) {
unordered_map<int, int> new_dp;
for (const auto& [x, dp_x] : dp) {
for (const auto& [y, cnt_y] : cnt) {
new_dp[x ^ y] = max(new_dp[x ^ y], dp_x + cnt_y);
}
}
dp = move(new_dp);
}
return size(nums) - dp[0];
}
int one_are_not_from_nums(const vector<int>& nums, const vector<unordered_map<int, int>>& cnts) {
static const auto& cmp =
[](auto x, auto y) {
return x.second < y.second;
};
vector<int> mxs;
int tot_mx = 0, mn_mx = numeric_limits<int>::max();
for (const auto& cnt : cnts) {
int mx = max_element(cbegin(cnt), cend(cnt), cmp)->second;
tot_mx += mx;
mn_mx = min(mn_mx, mx);
}
return size(nums) - (tot_mx - mn_mx);
}
};