Optimising a Python script -
i've been trying complete below task in python:
http://codeforces.com/problemset/problem/4/c
i created simple script can seen below, returns runtime error 7th test. believe due perhaps code taking long, require assistance optimising it. have looked @ map , filter commands , tried implementing them, without success.
a=int(input()) entered_usernames=[] n=0 while n<a: y=input() entered_usernames.append(y) n+=1 valid_usernames=[] in entered_usernames: if not in valid_usernames: valid_usernames.append(i) print('ok') else: count=1 while i+str(count) in valid_usernames: count+=1 valid_usernames.append(i+str(count)) print(i+str(count))
you can try changing valid_usernames
set
instead of list
.
for list list_a
operation x in list_a
takes (on average) linear time.
for set set_a
operation x in set_a
takes (on average) constant time.
(source: https://wiki.python.org/moin/timecomplexity)
this simple change improve runtime bit.
what strikes me potentially slow fragment:
while i+str(count) in valid_usernames: count+=1
however, if want improve this, need think using different data structure.
Comments
Post a Comment