-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimizeTheMaximumOfTwoArrays.txt
37 lines (34 loc) · 1.42 KB
/
minimizeTheMaximumOfTwoArrays.txt
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
2513. Minimize the Maximum of Two Arrays
/*
We have two arrays arr1 and arr2 which are initially empty.
You need to add positive integers to them such that they satisfy all the following conditions:
arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1.
arr2 contains uniqueCnt2 distinct positive integers, each of which is not divisible by divisor2.
No integer is present in both arr1 and arr2.
Given divisor1, divisor2, uniqueCnt1, and uniqueCnt2, return the minimum possible maximum integer that can be present in either array.
*/
class Solution
{
public:
long long LCM(long long x, long long y)
{
return x * y / __gcd(x, y);
}
int CalculateMax(int count, long long divisor1, long long divisor2)
{
long long LCMresult = LCM(divisor1, divisor2);
return count + count / (LCMresult - 1) - (count % (LCMresult - 1) ? 0 : 1);
}
int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2)
{
auto CalculateMaxLambda = [&](int count, long long d1, long long d2 = 1)
{
long long LCMresult = LCM(d1, d2);
return count + count / (LCMresult - 1) - (count % (LCMresult - 1) ? 0 : 1);
};
return max({
CalculateMaxLambda(uniqueCnt1, divisor1),
CalculateMaxLambda(uniqueCnt2, divisor2),
CalculateMaxLambda(uniqueCnt1 + uniqueCnt2, divisor1, divisor2)});
}
};