Forgive me if I come off as a bit novice, I started teaching myself how to code just a few weeks ago.
I've been working through a problem set at cses.fi. I came across the "collecting numbers" problem which essentially asks us: given an array, such as [4, 2, 1, 5, 3], how many times would we have to "collect" the numbers such that the numbers are organized in increasing order from 1 to n.
In this particular case we would need 3 rounds, as we iterate once and collect "1", a second iteration gives us "2, 3", and finally a third will give us "4, 5".
I am programming in Python and came across the solution eventually, though I was getting a TLE error when submitting it. I made a number of tweaks and discovered that .method() was slowing my code considerably.
TLE code:
n = int(input())
arr = list(map(int, input().split()))
i_arr = [0] * (n + 1)
res = 1
for i in arr:
i_arr[i] = arr.index(i)
for i in range(1, len(i_arr)):
if i_arr[i] < i_arr[i - 1]:
res += 1
print(res)
Accepted code with 0.09s run time:
n = int(input())
arr = list(map(int, input().split()))
i_arr = [0] * (n + 1)
for i in range(n):
i_arr[arr[i]] = i
res = 1
for i in range(1, len(i_arr)):
if i_arr[i] < i_arr[i - 1]:
res += 1
print(res)
The only difference between these two blocks of code are in the way I index in my first for loops.
My question is: is the ,index method slow in python? and if yes, why exactly? Thanks for any help.