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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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!