Programmers - 정렬
bool comp(string a, string b) {
int aIdx = 0, bIdx = 0;
while (true) {
if (a[aIdx % a.size()] > b[bIdx % b.size()])
return true;
else if (a[aIdx % a.size()] < b[bIdx % b.size()])
return false;
else {
if (aIdx < a.size())
aIdx++;
if (bIdx < b.size())
bIdx++;
}
if (aIdx == a.size() && bIdx == b.size())
break;
}
// 짤은 문장의 마지막 글자와 긴 문장의 마지막 글자가 같은 경우
if (aIdx > bIdx) {
return (a[a.size() - 1] >= b[0]);
}
else {
return (b[b.size() - 1] >= a[0]);
}
//return aIdx > bIdx;
}
처음에는 각기다른 두 문장의 문자를 일일이 비교하며 큰 것과 작은 것을 비교하였지만
그럴 필요가 없다고 느껴 두 문장을 합쳐 비교하여 답을 구하면 쉽게 구할 수 있다.
문제에 대한 유연한 사고가 필요하였다.
bool comp2(const string& a, const string& b) {
if (a + b > b + a)
return true;
return false;
}
연습장 풀이를 옮겨 적은 코드의 오타를 찾느라 오래걸림, 옮겨적은 풀이가 틀리면 오타가 있는지 확인하자.
int solution(vector<int> citations) {
int answer = 0, tmp = 0;
sort(citations.begin(), citations.end());
for (int i = 0; i < citations.size(); i++) {
if (citations[i] <= (citations.size() - i))
answer = citations[i];
else {
tmp = citations.size() - i;
break;
}
}
return answer < tmp ? tmp : answer;
}
현재 인용횟수가 남은 인용횟수 갯수 이하인 경우 현재 인용횟수가 H-Index가 될 수 있다.
현재 인용횟수가 남은 인용횟수 갯수 이상인 경우
댓글남기기