Untitled

 avatar
unknown
plain_text
a year ago
585 B
1
Indexable
class Solution {
public:
    int solve(int x,int y,vector<int>&dp){
        if(x==y) return 0;
        if(x<=y) return y-x;
        int res=abs(x-y);

        if(dp[x]!=-1) return dp[x];
        res=min(res,x%5 + 1+solve(x/5,y,dp)); //div to 5 below x
        res=min(res,(5-x%5) + 1+solve(x/5+1,y,dp));

        res=min(res,x%11 + 1+solve(x/11,y,dp)); //div to 11 below x
        res=min(res,(11-x%11) +1+ solve(x/11+1,y,dp));
        return dp[x]=res;
    }

    int minimumOperationsToMakeEqual(int x, int y) {
        vector<int>dp(10001,-1);
        return solve(x,y,dp);
    }
};
Leave a Comment