liveoverflow's blog

By liveoverflow, history, 4 years ago, In English

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);
    }
}
  • Vote: I like it
  • -12
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In your submission, you have created the array(vector) of size 200020 and n = 1e6, that's why you are getting RE.