시간 복잡도 : 6 10 10^6 = 6*10^7
4번에서 50만이 아니라 100만까지 하는 이유는 채널이 50만일때 0에서 가는 경우와 100만에서 가는 경우를 고려해야 하기 때문이다
정답 코드
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, btn[10];
bool btnBroken(string s) {
for(int i=0; i<s.size(); i++) {
int temp = s[i]-'0';
if(btn[temp]==1) {
return 1;
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0; i<m; i++) {
int in;
cin>>in;
btn[in]=1;
}
//1. save diff between n and 100 (use +, - only)
int ans=abs(n-100);
if(ans<3) {
cout<<ans<<'\n';
exit(0);
}
//2. select channel and calculate diff
for(int i=0; i<=1000000; i++) {
string s=to_string(i);
if(btnBroken(s)) { //don't consider if btn is broken
continue;
}
else {
int cand = s.size();
cand+=abs(i-n);
ans = min(ans, cand);
}
}
cout<<ans<<'\n';
return 0;
}