forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-time-required.py
73 lines (54 loc) · 1.75 KB
/
minimum-time-required.py
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
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
def calculate_days(day, cnt):
res = 0
for el in cnt.items():
res += el[1] * (day // el[0])
return res
# Complete the minTime function below.
def minTime(machines, goal):
cnt = Counter(machines)
day_limit = 10**13
curgoal = 0
curday = 0
res = 0
prev_big = day_limit
prev_low = 0
curday = day_limit // 2
while True:
# detect if we are bouncing around in a loop
if curday == prev_big or curday == prev_low:
#print("curday = {} prev_low = {} prev_big = {}".format(curday, prev_low, prev_big))
prev_low_res = calculate_days(prev_low, cnt)
prev_big_res = calculate_days(prev_big, cnt)
if prev_low_res >= goal:
return prev_low
else:
return prev_big
curgoal = calculate_days(curday, cnt)
if curgoal < goal:
prev_low = curday
#print("micro curday = {} curgoal = {} < {}, diff = {}".format(curday, curgoal, goal, abs(curgoal - goal)))
curday = curday + (1 + day_limit - curday)//2
res = curday
else:
#print("micro curday = {} curgoal = {} > {}, diff = {}".format(curday, curgoal, goal, abs(curgoal - goal)))
prev_big = curday
day_limit = curday
curday = curday // 2
res = curday
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nGoal = input().split()
n = int(nGoal[0])
goal = int(nGoal[1])
machines = list(map(int, input().rstrip().split()))
ans = minTime(machines, goal)
fptr.write(str(ans) + '\n')
fptr.close()