forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimize-the-maximum-of-two-arrays.cpp
43 lines (39 loc) · 1.34 KB
/
minimize-the-maximum-of-two-arrays.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(log(min(d1, d2)))
// Space: O(1)
// number theory
class Solution1 {
public:
int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {
const auto& count = [](int cnt, int64_t d1, int64_t d2) {
const int64_t l = lcm(d1, d2);
return cnt + cnt / (l - 1) - static_cast<int>(cnt % (l - 1) == 0);
};
return max({count(uniqueCnt1, divisor1, 1),
count(uniqueCnt2, divisor2, 1),
count(uniqueCnt1 + uniqueCnt2, divisor1, divisor2)});
}
};
// Time: O(log(min(d1, d2)) + logr)
// Space: O(1)
// binary search
class Solution2 {
public:
int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {
const int64_t l = lcm(static_cast<int64_t>(divisor1), static_cast<int64_t>(divisor2));
const auto& check = [&](int cnt) {
return cnt - cnt / divisor1 >= uniqueCnt1 &&
cnt - cnt / divisor2 >= uniqueCnt2 &&
cnt - cnt / l >= uniqueCnt1 + uniqueCnt2;
};
int left = 2, right = numeric_limits<int>::max();
while (left <= right) {
const int mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};