Можно ли быть лучше, чем O (N + M) для урока Codility MaxCounters, используя Python?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Можно ли быть лучше, чем O (N + M) для урока Codility MaxCounters, используя Python?

Сообщение Anonymous »


This is the code I am using for the Codility lesson: MaxCounters

def solution(N, A): counters = [0] * N max_c = 0 for el in A: if el >= 1 and el N: counters = [max_c] * N return counters Every test passes but the last one ("all max_counter operations") times out after 7 seconds so the result is just 88% with time complexity of O(N+M).

Is it possible to improve the time complexity of the algorithm and get 100% test result using Python?
The MaxCounters task
You are given N counters, initially set to 0, and you have two possible operations on them:
  • increase(X) − counter X is increased by 1,
  • max counter − all counters are set to the maximum value of any counter.

A non-empty array A of M integers is given. This array represents consecutive operations:
  • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  • if A[K] = N + 1 then operation K is max counter.

Write an efficient algorithm for the following assumptions:
  • N and M are integers within the range [1..100,000];
  • each element of array A is an integer within the range [1..N + 1].


Источник: https://stackoverflow.com/questions/588 ... s-using-py
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Статус урока Scorm «в процессе» даже после завершения чтения урока
    Anonymous » » в форуме IOS
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • Проблема Codility OddOccurrencesInArray — рекурсия и Python
    Anonymous » » в форуме Python
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • Найдите недостающее целое число в Codility
    Anonymous » » в форуме C#
    0 Ответы
    18 Просмотры
    Последнее сообщение Anonymous
  • Как правильно решить тест PermMissingElem от Codility? (Ява)
    Anonymous » » в форуме JAVA
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • Почему тест Perm-Missing-Elem Codility возвращает для моего кода результат 66/100 и показывает «Недопустимый тип результ
    Anonymous » » в форуме Php
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»