Here is the problem link : https://www.hackerrank.com/challenges/down-to-zero-ii/problem
I used bottom up dp , but still getting stack overflow runtime error for input 1000000 . Can anyone help me , to point out where is wrong in my code :
code
# | 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 |
Here is the problem link : https://www.hackerrank.com/challenges/down-to-zero-ii/problem
I used bottom up dp , but still getting stack overflow runtime error for input 1000000 . Can anyone help me , to point out where is wrong in my code :
# include < bits/stdc++. h >
using namespace std;
int n,q,i,j;
int dp[2000000];
int flag[2000000];
int recursion(int N) {
// cout<<N<<endl;
if(N==0) return 0; if(dp[N]!=-1) return dp[N]; int x=recursion(N-1)+1; int y=2147483647; for(int i=2; i*i<=N; i++) if(N%i==0) y=min(y,recursion(N/i)+1); dp[N]=min(x,y); return dp[N];
}
int main() {
memset(dp,-1,sizeof(dp)); scanf("%d",&q); while(q--) { scanf("%d",&n); printf("%d\n",recursion(n)); } return 0;
}
Name |
---|
Is this problem impossible to solve with Bottom up DP ? I solved using top down dp , using queue .
for $$$N=0$$$ you return $$$n$$$? shouldn't this be $$$0$$$?
This is bottom up, gets accepted and its more or less in $$$O(n\sqrt{n})$$$:
This is $$$O(n\cdot log(n))$$$, as it can be written as $$$\sum_{i=1}^n\frac{n}{i} \simeq n \cdot ln(n) = O(n \cdot log(n))$$$
oh yes you are right.
Yeah, it should be 0. But still it overflow on 1000000
and you probably want to initialize $$$dp$$$ with $$$0$$$ and not $$$-1$$$ ?