Skip to content

Instantly share code, notes, and snippets.

@balamark
Created November 14, 2017 02:06
Show Gist options
  • Save balamark/b277e196de0a88a34562950f2b9921e1 to your computer and use it in GitHub Desktop.
Save balamark/b277e196de0a88a34562950f2b9921e1 to your computer and use it in GitHub Desktop.
first try. passed all except 999999937(TLE)
class ProductOfDigits {
public:
int smallestNumber(int N) {
vector<int> factors;
int n = N;
for(int i=2;i<=N;++i){
while(n%i==0){
factors.push_back(i);
n/=i;
}
if(n==1) break;
}
if(factors.size()==0) return 1; // 1
int ret=0, c=1;
for(auto f : factors){
if(f>9) return -1; //2 digits factor
c *= f;
if(c>9){
c=f;
ret++;
}
}
if(c>1) ret++; //last one
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment