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 | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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!