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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
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;
}
Название |
---|
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$$$ ?