CodeJam Round B Robot Path Decoding

Revision en2, by ritik652000, 2020-04-20 07:47:45

Can someone please tell me why my O(N) Approach got TLE. Thankyou

Code

for _ in range(int(input())):
    p = input()
    a,b = 1,1
    con = []
    oper = []
    i = 0
    while i<len(p):
        if(p[i]==')'):
            j = oper.pop()
            a*=j
            b*=j
            a+=con[-1][0]
            b+=con[-1][1]
            con.pop()
        elif(p[i]=='E'):
            a+=1
        elif(p[i]=="N"):
            b-=1
        elif(p[i]=="S"):
            b+=1
        elif(p[i]=="W"):
            a-=1
        else:
            oper.append(int(p[i]))
            con.append([a,b])
            a,b = 0,0
            i+=1
        i+=1
    if(a<1):
        a = 1000000000+a
    if(b<1):
        b = 1000000000+b
    a = a%1000000000
    b = b%1000000000
    if(a==0):
        a = 1000000000
    if(b==0):
        b = 1000000000    

    print("Case #",_+1,": ",sep = "",end = "")
    print(a,b)
Tags #googlekickstart, #tle, #python3

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English ritik652000 2020-04-20 14:59:55 14
en2 English ritik652000 2020-04-20 07:47:45 443 Tiny change: 'put())):\n\n ' -> 'put())):\n '
en1 English ritik652000 2020-04-20 07:33:37 1123 Initial revision (published)