Блог пользователя jay__0208

Автор jay__0208, история, 6 лет назад, По-английски

Recently I gave Codeforces round #660 (Div 2) ============================================= ,

where i solved problem B in O(n) time complexity After doing the problem in O(n) I still get TLE !!! and its not only me I get to know many other also faced this problem

EVEN C++ O(n) SUBMISSION GOT TLE !!!!!

My submission : http://codeforces.me/contest/1388/submission/88472454

A C++ Submission : https://codeforces.me/contest/1388/submission/88470034

  • Проголосовать: нравится
  • -48
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by jay__0208 (previous revision, new revision, compare).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

For 1, I think it's PyPy.

I tested the code in custom test. With Python 3.7.2 I got 124ms, but with PyPy 3.6 I got 2261ms.

For 2, It's $$$\mathcal{O}(n^2)$$$. string::operator= is $$$\mathcal{O}(n)$$$ and you used it $$$\mathcal{O}(n)$$$ times. The correct way is using string::operator+=.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

The runtime of Python string concatenation is the length of the combined string, not O(1)

https://stackoverflow.com/questions/37133547/time-complexity-of-string-concatenation-in-python

You code therefore runs in O(n2).

You will get familiar with Python data structures over time. Today I learnt how to call a function recursively in Codeforces.

»
6 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +4 Проголосовать: не нравится

In python, strings are immutable.

When you concatenate strings then it creates a new instance. So every time you add a character to a string it creates a new instance of that string which takes O(n) time. And you do this n times so O(n*n). It's always better to use list instead of string in such cases.

alternative 1
alternative 2
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Please don't shout.