[Picking Numbers - 풀이]
더보기
#define pp pair<int, int>
bool comp(pp& left, pp& right)
{
return left.first < right.first;
}
int pickingNumbers(vector<int> a) {
int result = 0;
// set arr to map
map<int, int> arr;
for(size_t i = 0 ; i < a.size(); ++i)
arr[a[i]]++;
// change format for sort
vector<pp> v(arr.begin(), arr.end());
sort(v.begin(), v.end(), comp);
//
auto iter = v.begin();
auto next = iter + 1;
auto end = v.end();
//
if(iter == end)
return (iter->second);
//
if(next == end)
return (iter->second + next->second);
//
for( ; next != end ; ++iter, ++next)
{
if(next != end)
{
if(iter->second > result)
result = iter->second;
if(next->first - iter->first <= 1)
{
int temp = iter->second + next->second;
if(temp > result)
result = temp;
}
}
}
//
return result;
}
[Save the Prisoner! - 풀이]
더보기
int saveThePrisoner(int prisoner, int treat, int start)
{
long long result = 0;
//
long long n = prisoner;
long long m = treat;
long long s = start;
//
result = ((((s-1) + (m%n)) + (n-1)) % n) + 1;
//
return result;
}
- 결과가 너무 이상해서 테스트케이스 까봤더니 overflow가 발생해있었다..