This article takes you to thoroughly understand why the program timed out


Some students may have no idea about the speed of the computer

It may be that the computer should run fast

So why do we time out when doing algorithm problems?

How many times can our computer calculate in 1s?

Next, let's discuss these issues.

What is the timeout

Insert picture description here

Everyone should have encountered a kind of error when they brushed the leetcode is a timeout

In other words, the program runs longer than the specified time, and leetcode does not say how long the program has been running, nor does it say what the timeout is.

Generally, the timeout time of the judgment system is 1s, and other OJs, such as POJ or ZOJ, are basically 1s.

That is to say, it takes at most 1s to get the result after the use case data is entered, and the leetcode should also be about 1s (the limit of each question on the leetcode may be different).

For the convenience of explanation below, the tentative timeout is 1s

Next we need to know why our code times out

That is, if we write an O (n) algorithm, we can actually estimate how big n is, and the execution of our algorithm will exceed 1s.

If we know that the size of n is enough for O (n) algorithm to run for more than 1s, then we should consider the log (n) solution

Guess you like

Origin blog.csdn.net/youngyangyang04/article/details/105323440
Recommended