I was solving Book Shop of CSES DP section.
- I am getting TLE even after multiple attempts, Can you tell me how to optimize my TOP-DOWN approach.
- I have tried replacing 2-d array with 2-d vector .
- I have also tried swapping the dimensions of dp array ( some caching thing ).
my code
Everything is fine. Just set dp[index][total] = ans before returning.
it is referenced with variable named ans,so i guess it should be fine, right??
The time constraints in the CSES Problem set are very tight! In some problems using
long long
instead ofint
might also give TLE. So its always better to use Iterative method rather than Recursive(for CSES)yeah i read it somewhere, so should i assume that it is unsolvable using TOP-DOWN approach(on CSES)??
It might work for some Problems I'm not too sure about that but I do remember trying top_down approach for some Problems and got TLE too! Not that the solution was wrong, but the time constraints were just too tight for recursive solutions!! So yeah if you want AC in CSES then you have to solve iteratively(atleast for most of them or even all of them)!
I kept on getting runtime error on my bottom up dp solution just because I used long long
It's actually Memory Limit Exceeded.
yes
same
this worked for me...
bro can you paste the AC code (recursive one)
Sorry I haven't written recursive . But iterative one :
include<bits/stdc++.h>
using namespace std;
int mod = 1e9 +7; int main(){ int n,x; cin>>n>>x; pair<int,int> h[n];
cout<<dp[n][x]<<"\n"; }
I am using iterative way but still getting tle what could be the reason here is the code
include
include
using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(0);
}
Your code seems perfect to me. Did you find the error?
Yes.
Surprisingly, my solution with a complexity of O(1e8) was accepted, and it ran in only 0.1 seconds.
can you paste your code here
I'm surprised that this is for 3 years ago but the latest comments are from 2 weeks ago.
I just got accepted on the task and I had the same problem. I mean technically it should've been an MLE. I dont know if anyone has said this before or not, but here instead of defining a DP[100000][100] I just defined a DP[100000][2] since you only need dp[j][i-1] to update your DP, So you can just store the previous row of your DP table and use parity to go over the loop and store your values.
here's my code: