Time limit exceeded python

Что поправить в коде, чтоб он проходил по времени.
Вот условие задачи:

You are given an array of n numbers and q queries. For each query you
have to print the floor of the expected value(mean) of the subarray
from L to R.
Input:
First line contains two integers N and Q denoting number of array elements and number of queries. Next line contains N space
seperated integers denoting array elements. Next Q lines contain two
integers L and R(indices of the array).
Output:
print a > single integer denoting the answer.
Constraints:
1 Добавить комментарий

Решаю я значит задачу на Stepic. И у меня возникла проблема. Вроде как задача простейшая, написал код, который при моей проверке на рандомных значениях выдает все нормально. А вот Stepic выдает ошибку Failed test #3. Time limit exceeded. Может кто разбирался с этой задачей, или прсото сможет понять, в чем дело. Код прикреплю ниже на всякий случай

1 ответ 1

По условиям задачи функция f(x) вычисляется очень долго, чтобы пройти тест нужно кэшировать результат выполнения и если на вход приходит значение x которое уже вычислялось, нужно брать значение из кэша.

Вы кэшируете резальтат, но никак не используете закэшированный ответ.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками python-3.x или задайте свой вопрос.

Похожие

Для подписки на ленту скопируйте и вставьте эту ссылку в вашу программу для чтения RSS.

дизайн сайта / логотип © 2020 Stack Exchange Inc; пользовательское содержимое попадает под действие лицензии cc by-sa 4.0 с указанием ссылки на источник. rev 2020.1.17.35809

Cons >

  1. All elements in $A$ are factors of $x$.
  2. $x$ is a factor of all elements in $B$.

Given $A$ and $B$, find and print the number of integers (i.e., possible $x$’s) that are between the two sets.

Assuming lists a and b as in the problem, the main idea is finding the LCM of list a and the GCD of list b , and then finding out how many multiples of the LCM divide the GCD perfectly without remainders. I ended up with this –

But I pass only 7 of the 8 test cases, the last one getting terminated due to time out. I don’t understand which part of my code would result in a long loop that might end up in a timeout.

Оцените статью
ПК Знаток
Добавить комментарий

Adblock
detector