[Geeks for Geeks] Find the minimum time

Find the minimum time

Geek wants to scan N documents using two scanners. If S1 and S2 are the time taken by the scanner 1 and scanner 2 to scan a single document, find the minimum time required to scan all the N documents.

  • Time : O(log(min(t1, t2) * n))
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution{
public:
int minTime(int t1, int t2, int N){
int l = 0, r = min(t1, t2) * N, res = INT_MAX;
while(l <= r) {
int m = l + (r - l) / 2;
int scan = m / t1 + m / t2;

if(scan >= N) res = min(res, m);
if(scan >= N) r = m - 1;
else l = m + 1;
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/20/PS/GeeksforGeeks/find-the-minimum-time/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.