https://mirror.codeforces.com/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://mirror.codeforces.com/problemset/problem/1251/C
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
https://mirror.codeforces.com/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://mirror.codeforces.com/problemset/problem/1251/C
Name |
---|
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!