Блог пользователя liveoverflow

Автор liveoverflow, история, 5 лет назад, По-английски

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);
    }
}
  • Проголосовать: нравится
  • -12
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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