Help, segmentaion fault in dp problem

Revision en2, by liveoverflow, 2020-05-22 13:50:26

Q)subtract a digit from the given number (the digit must occur in the number) and get a new number. Repeat this operation until the number equals zero. count the minimum number of operations needed to reduce the number to zero.

submission link -> 80890640 for n = 1000000, the given code is giving segmentation fault but works well for other values. Please, anyone me help to get rid of this

vector<int>dp(1000000+1,1e9);
int f(int n){
    if(dp[n]!=1e9) return dp[n]; 
    int te = n;
    while(te){
     if(te%10!=0) dp[n]=min(dp[n],1+f(n-te%10));
     te/=10;
    } 
     return dp[n];
}
int main(){
    int t=1; //cin>>t;
    while(t--){
       int n; cin>>n;
        dp[0]=0;
        cout<< f(n);
    }
}
Tags #dynamic programing, #dp, #help, segmentation fault

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English liveoverflow 2020-05-22 13:50:26 136
en1 English liveoverflow 2020-05-22 13:46:59 668 Initial revision (published)