-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestTimeToParty.py
44 lines (34 loc) · 1.14 KB
/
bestTimeToParty.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
"""
A list of celebrities are arriving and we have their in and out times.
You are allowed to party for an hour.
What time should you go to party?
"""
n = int(input("Number of celebrities : "))
timing = []
for i in range(n):
print("Enter time for celebrity number ", i+1)
in_time = int(input("In time : "))
out_time = int(input("Out time : "))
timing.append([in_time, out_time])
def convert_into_start_and_leave_timings():
all_timing = []
for i in range(len(timing)):
all_timing.append([timing[i][0], 'enter'])
all_timing.append([timing[i][1], 'exit'])
return all_timing
get_timings = convert_into_start_and_leave_timings()
get_timings.sort()
def find_best_time():
best_time = [float('-inf'), -1]
curr_best = 0
for i in range(len(get_timings)):
if(get_timings[i][1] == 'enter'):
curr_best += 1
else:
curr_best -= 1
if(curr_best > best_time[0]):
best_time[0] = curr_best
best_time[1] = get_timings[i][0]
print("Time is {} and number of celebrities will be {}".format(
best_time[1], best_time[0]))
find_best_time()