Difference between "t=int(input();for i in range(t)" and "for i in range(int(input())" ?

Revision en3, by tanu38, 2020-06-12 21:45:00

Hello everyone!

Recently while solving a question 313B (in Python3 ), Me and my friend observed that TLE comes when we directly iterate using input, but not when we store intput in a variable and then iterate.

Please explain the reason why this is happening!

Case 1: TLE occured a = input() w=[0] z,j =0,1 for i in range(1,len(a)): if a[i]==a[i-1]: z+=1 w.append(z) for i in range(int(input())): a,b = map(int,input().split()) print(w[b-1]-w[a-1])

Case 2: No TLE, got Accepted a = input() w=[0] z,j =0,1 for i in range(1,len(a)): if a[i]==a[i-1]: z+=1 w.append(z) t =int(input()) for i in range(t): a,b = map(int,input().split()) print(w[b-1]-w[a-1])

Tags #python3, #tle

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English tanu38 2020-06-12 21:45:00 489
en2 English tanu38 2020-06-12 21:41:24 140
en1 English tanu38 2020-06-12 21:38:11 354 Initial revision (published)