I wrote a recursive solution with O(n^3) time complexity imo.
But I am getting TLE Verdict on submission .
# | 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 | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
I wrote a recursive solution with O(n^3) time complexity imo.
But I am getting TLE Verdict on submission .
Name |
---|
The main issue is that the parameter
valX
andvalY
is thesolve
funciton may be negative, and lead to many useless states. Since we only care about whether we have enough x and y, so change LINE22 intoreturn dp[i][valX][valY] = min(1 + solve(i + 1, max(0, valX - v[i].ff), max(0, valY - v[i].ss)), solve(i + 1, valX, valY));
would just be fine.Thank u so much!
Still got a TLE. Submission
I got the following update.
Accepted
The default dp value should also be changed. Change
1e9
in LINE21 and LINE43 into-1
.Why taking -1 gets an AC whereas taking 1e9 as dp value gets a TLE.
AC submission
TLE Submission
Difference in the runtime is also huge. TLE submission takes 2.2s still no output whereas AC submission takes 0.3s with just this small change. Can you clarify?
In the TLE Submission, the default dp value is
1e9
. This means that when you call thesolve
function and the currentdp[i][valX][valY]
is 1e9, it keeps doing recursion.Assume current state is valX=10, valY=10, and the suffix of the input data is like
[..., {1,1}, {1,1}, {1,1}, {1,1}]
. We cannot subtract valX and valY to 0 even if we use all the suffix, so thedp[i][valX][valY]
is 1e9. When you visit this state again, LINE21 check that the dp value is 1e9 and still need to be searched. The runtime would be exponential to n in the worst case.Generally speaking, it's a bad idea to mix the
UNVISITED
status and theINVALID
status when we do dfs with memorization.