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.
Can someone please help me out.

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

Thanks in advance.

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.
Please help me, why I am getting such a error.

You should read my comment…

Hey, I did this
my solution link: CodeChef: Practical coding for everyone

# 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