-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
48 lines (46 loc) · 1.18 KB
/
Solution.cs
File metadata and controls
48 lines (46 loc) · 1.18 KB
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
public class Solution
{
public long MinNumberOfSeconds(int mountainHeight, int[] workerTimes)
{
int maxTime = 0;
foreach (int time in workerTimes)
{
maxTime = Math.Max(maxTime, time);
}
long lo = 1;
long hi = 1L * mountainHeight * (mountainHeight + 1) * maxTime;
long ans = hi;
while (lo <= hi)
{
long mid = lo + (hi - lo) / 2;
if (Ok(mid, mountainHeight, workerTimes))
{
ans = mid;
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
return ans;
}
bool Ok(long t, long h, int[] arr)
{
// t = x(x+1)/2 * worker[i];
// => x(x+1)/2 = t / worker[i];
// => x(x+1) = 2*t / worker[i];
// => x^2 <= 2*t / worker[i];
// => x <= sqrt(2*t / worker[i]);
long sum = 0;
foreach (int w in arr)
{
long v = 2 * (t / w);
long x = (long)Math.Sqrt(v);
if (x * (x + 1) > v) x--;
sum += x;
if (sum >= h) return true;
}
return sum >= h;
}
}