I have a question regarding time complexity in my codes. I tried to refer to a number of sites and tutorials and also stackoverflow explanations but still could not understand how we calculate or determine the time complexity of codes. If possible, is it ok to check my codes and explain based on it and also provide some EASY examples or links (Im still learning) to study it.
I tried this code and try to submit it but the time complexity is high so it is not accepted. Is there a way to reduce the time complexity too?
question is from this link
def large_element(array):
stack = []
took = False
for i in range(len(array)):
s_integer = array[i+1:]
if i != len(array)-1:
for ii in range(len(s_integer)):
if array[i] < s_integer[ii] and took == False:
stack.append(s_integer[ii])
took = True
elif array[i] > s_integer[ii] and took == False and ii == len(s_integer)-1:
stack.append(-1)
took = True
took = False
else:
stack.append(-1)
return stack
import time
start_time = time.time()
integer = [4,3,2,1]
print(large_element(integer))
My current understanding is that my code have 2 times for loop to loop each element so this will be O(n2)
?
By the way, the output is:
[-1, -1, -1, -1]