C++ solution [ O( vec.size(B) * limit length(C) ) ]


#1

int Solution::solve(vector &num, int len, int limit)
{
if (num.size() == 0 || len<1)
return 0;
int result = 0, equal=0;
int first_limit=limit;
while (limit > 0){
int MSN = limit / pow(10, len - 1);
int count=0;
for(int i=0;i<num.size();i++){
if (num[i]<MSN)
count++;
else if (num[i]==MSN)
equal = 1;
else
break;
}
if (count==0 && first_limit==limit && equal==0)
return 0;
result += count*pow(num.size(),len-1);

    if (first_limit==limit && num[0]==0 && len>1)
        result -= pow(num.size(),len-1) ;
    if (equal ==1){
        limit = limit - MSN * pow(10, len - 1);
        len--;
        equal = 0;
    } else{
        limit =0;
    }
}
if (result<0){ //in case B is bigger than C's length
    result=0;
}
return result;

}