forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-a-value-of-a-mysterious-function-closest-to-target.cpp
88 lines (80 loc) · 2.14 KB
/
find-a-value-of-a-mysterious-function-closest-to-target.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
// Time: O(nlogm), m is the max value of arr
// Space: O(logm)
class Solution {
public:
int closestToTarget(std::vector<int>& arr, int target) {
static const int LOGM = 20;
BitCount count(LOGM);
int result = numeric_limits<int>::max();
for (int left = 0, right = 0; right < arr.size(); ++right) {
count += arr[right];
while (left <= right) {
const auto& f = count.bitAnd();
result = min(result, abs(f - target));
if (f >= target) {
break;
}
count -= arr[left++];
}
}
return result;
}
private:
class BitCount {
public:
BitCount(int n)
: l_(0)
, n_(n)
, count_(n) {
}
int bitAnd() const {
int num = 0;
for (int i = 0; i < n_; ++i) {
if (count_[i] == l_) {
num |= 1 << i;
}
}
return num;
}
void operator+=(int num) {
++l_;
for (int i = 0; i < n_; ++i) {
if (num & (1 << i)) {
++count_[i];
}
}
}
void operator-=(int num) {
--l_;
for (int i = 0; i < n_; ++i) {
if (num & (1 << i)) {
--count_[i];
}
}
}
private:
int l_;
int n_;
vector<int> count_;
};
};
// Time: O(nlogm), m is the max value of arr
// Space: O(logm)
class Solution2 {
public:
int closestToTarget(vector<int>& arr, int target) {
int result = numeric_limits<int>::max();
unordered_set<int> dp; // at most O(logm) dp states
for (const auto& x : arr) {
unordered_set<int> new_dp = {x};
for (const auto& f: dp) {
new_dp.emplace(f & x);
}
for (const auto& f : new_dp) {
result = min(result, abs(f - target));
}
dp = move(new_dp);
}
return result;
}
};