-
Notifications
You must be signed in to change notification settings - Fork 0
/
37-MinimumTimeRequired-Searching-BinarySearch.cs
108 lines (99 loc) · 3.23 KB
/
37-MinimumTimeRequired-Searching-BinarySearch.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace HackerRank
{
class MinimumTimeRequired_Solution
{
//public static long MinimumTimeRequired(long[] machines, long goal)
//{
// long day = 0;
// int count = 0;
// Dictionary<long, long> Map = new Dictionary<long, long>();
// while(count != goal)
// {
// day++;
// for (int i = 0; i < machines.Length; i++)
// {
// if (day % machines[i] == 0)
// {
// count++;
// }
// }
// Map.Add(day, count); //this is not necessay, this is just to check the total records after everyday
// }
// return day;
//}
//public static long MinimumTimeRequired(long[] machines, long goal)
//{
// Array.Sort(machines);
// long n = machines.Length;
// /// according to constraint
// /// 1< machines[i]< 10^9
// long min = 1;
// long max = 1000000000; //10^9
// long guess = 0;
// while (min <= max)
// {
// guess = (long)(Math.Floor(Convert.ToDecimal((min + max) / 2)));
// long count = 0;
// for (int i = 0; i < n; i++)
// {
// count += guess / machines[i];
// }
// if(count == goal)
// {
// return guess-1; //as initial min value is 1 not 0 that is why we are doing -1, In binary search usually we search from 0.
// }
// if (count < goal)
// {
// min = guess + 1;
// }
// else
// {
// max = guess - 1;
// }
// }
// return -1;
//}
public static long MinimumTimeRequired(long[] machines, long goal)
{
Array.Sort(machines);
long left = machines[0];
long right = machines[0] * goal;
while (left <= right)
{
long mid = (left + right) / 2;
long count = 0;
for (int i = 0; i < machines.Length; i++)
{
count += mid / machines[i];
}
if (count >= goal)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
return left;
}
}
class MinimumTimeRequired_Result
{
public static void Input()
{
//long goal = 10;
//long[] machines = new long[] { 1, 3, 4 };
//output = 7
long goal = 12;
long[] machines = new long[] { 4, 5, 6 };
//output= 20
long minDays = MinimumTimeRequired_Solution.MinimumTimeRequired(machines, goal);
Console.WriteLine($"Min days are required: {minDays}");
}
}
}