https://codeforces.net/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://codeforces.net/problemset/problem/1251/C
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3839 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | Geothermal | 3569 |
8 | ecnerwala | 3494 |
9 | Um_nik | 3396 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 164 |
2 | -is-this-fft- | 160 |
2 | maomao90 | 160 |
4 | atcoder_official | 158 |
4 | cry | 158 |
4 | awoo | 158 |
7 | adamant | 155 |
8 | nor | 154 |
9 | TheScrasse | 153 |
10 | Dominater069 | 152 |
https://codeforces.net/contest/1251/submission/76981021 can someone tell why this code is giving TLE? the complexity seems fine to me?
question link... https://codeforces.net/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!