Skip to content

Instantly share code, notes, and snippets.

@medvednikov
Last active October 3, 2019 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medvednikov/8fd965c5158b23b4ea44ef8f1f7f5c22 to your computer and use it in GitHub Desktop.
Save medvednikov/8fd965c5158b23b4ea44ef8f1f7f5c22 to your computer and use it in GitHub Desktop.
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
@medvednikov
Copy link
Author

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment