Hi codeforces community!
So I was solving this problem: 1703G - Good Key, Bad Key
I wrote first solution: 283940469 ~ TLE
I wrote second solution: 283940935 ~ AC
Two programs are very similar to each other so i will list most notable differences between them:
- First solution uses
C-arrayaof sizeN = 1e5defined globally. Second solution usesvectorof sizenwhich is defined for each test case - First solution uses
vector<vector<ll>>dp, withNrows and each row is of size31and for each test it fills every row withvector<ll>(31, -1). Second solution uses similardpvector but hasnrows and is defined in thesolvefunction - First solution accesses variables
n,k,dpfrom the global scope. Second solution passes references tovectorsas parameters and variablekalso as parameters for each recursive call.
I sincerely thought first solution would be faster than second solution.
So, my question is why is that first solution gets TLE but second solution gets AC?








In first solution you are refilling the whole $$$dp$$$ array
So for each test case it does at least N operations (which will lead to TLE when you have 10^4 testcases)
You reset your
dpvector in each test case. In the first submission yourdpsize is $$$N$$$ so you'll get $$$O(TN)$$$ complexity, which reaches $$$10^9$$$.