https://codeforces.me/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://codeforces.me/problemset/problem/1251/C
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
https://codeforces.me/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://codeforces.me/problemset/problem/1251/C
Название |
---|
Auto comment: topic has been updated by nipungupta37 (previous revision, new revision, compare).
Because you worst case time complexity is O(n^2).
isnt it O(10*n)?
Look at this line from your code (there are 3 such lines):
At worst, this is executed $$$n$$$ times per iteration of the outer loop. Each time, the program creates the concatenation of
s3
ands[j]
, which could take $$$O(n)$$$ time, then assigns this concatenation tos3
, which takes another $$$O(n)$$$ time to delete the old version ofs3
and assign the new string. To fix this, you would want to dos3 += s[j]
, which doesn't re-create the string every time, or even better:s3.push_back(s[j])
, which is faster performance-wise.i corrected it and got AC. Thank you so much!