Last active
October 3, 2019 00:08
-
-
Save medvednikov/8fd965c5158b23b4ea44ef8f1f7f5c22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(S): | |
lines = S.split('\n') | |
dict = {} | |
for i in xrange(len(lines)): | |
line = lines[i] | |
if len(line) == 0: | |
continue | |
# Parse values | |
vals = line.split(',') | |
hh, mm, ss = vals[0].split(':') | |
phone = vals[1] | |
seconds = int(ss) + int(mm) * 60 + int(hh) * 3600 | |
# Add seconds to phone number | |
if phone not in dict: | |
dict[phone] = 0 | |
dict[phone] += seconds | |
# Find a phone with most seconds | |
max = 0 | |
max_phone = '' | |
for key, val in dict.items(): | |
if val > max: | |
max = val | |
max_phone = key | |
# Handle a tie | |
if val == max: | |
if key < max_phone: | |
max = val | |
max_phoney = key | |
# Calculate the cost | |
cost = 0 | |
for phone, seconds in dict.items(): | |
if seconds == max: | |
continue | |
if seconds < 300: | |
cost += seconds * 3 | |
elif seconds >= 300: | |
mins = seconds // 60 | |
if seconds % 60 > 0: | |
mins += 1 | |
cost += mins * 150 | |
return cost | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice