# Contest Code:PRACTICE Problem Code:TRAINSET

I was trying to solve the above problem, but on submitting I am getting Time Limit Exceed error. I have tried test cases, and its giving me the desired output. But, on submitting facing this Time Limit issue.

This is the code which I wrote -:
try:

    T = int(input())

for k in range(0,T):
N = int(input())

alpha = []
for j in range(0,N):
word,boolean = input().split()
boolean = int(boolean)

if len(alpha) == 0:
alpha.append([word,boolean])
else:
for i in alpha:
if i[0] == word and i[1] == boolean:
alpha.append([word,boolean])
break
elif i[0] == word and i[1] !=boolean:
continue
else:
alpha.append([word,boolean])
break

print(len(alpha))

except:
pass


This is the link for my solution
https://www.codechef.com/viewsolution/35226030

1 Like

I donâ€™t really get your logic or how itâ€™s correct (it seems like it shouldnâ€™t be), but if you have \frac{n}{2} pairs of (a, 0) and \frac{n}{2} pairs of (a, 1), I think that will cause your program to be O(n^2)

See this the screenshot, I have tried the sample input given in the example and I got the desired outputs, even it takes less time.

Hey, I did this

# cook your dish here
for _ in range(int(input())):
n=int(input())
d={}
dd={}
for i in range(n):
l=input().split()
if l[0] in d:
if int(l[1])==0:
d[l[0]]+=1
else:
d[l[0]]=0
if int(l[1])==0:
d[l[0]]+=1
if l[0] in dd:
if int(l[1])==1:
dd[l[0]]+=1
else:
dd[l[0]]=0
if int(l[1])==1:
dd[l[0]]+=1
l=list(d.values())
k=list(dd.values())
s=0
for i in range(len(l)):
s=s+max(l[i],k[i])
print(s)


Please let me know if you donâ€™t get anything.
Happy to help.

1 Like