2. 가장 큰 수
코드와 같은 comp함수를 생각해내냐 못해냐에 따라 크게 달라지는 완성도
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool comp(string a, string b){
return a+b>b+a;
}
string solution(vector<int> numbers) {
string answer = "";
vector<string> tmp;
for(int i=0; i<numbers.size(); i++)
tmp.push_back(to_string(numbers[i]));
sort(tmp.begin(), tmp.end(), comp);
for(int i=0; i<tmp.size();i++)
answer+=tmp[i];
if(answer[0]=='0') return "0";
return answer;
}
3. H-Index
주석처럼 생각했는데 왜 답처럼 생각하지 못했을까
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> citations) {
int answer = 0;
sort(citations.begin(), citations.end(), greater<int>());
for(int i=0; i<citations.size(); i++){
if(citations[i] > (i+1)) answer++;
/*if(citations[i] <= (i+1)){
answer = i+1;
break;
}*/
}
return answer;
}
'알고리즘 > 알고리즘' 카테고리의 다른 글
[C++] 백준 20413번 : MVP 다이아몬드 (Easy) (0) | 2022.05.08 |
---|---|
[C++] 프로그래머스 : 소수찾기 (0) | 2022.03.10 |
[C++] 프로그래머스 : 코딩테스트연습_힙 (0) | 2022.02.15 |
[C++] 프로그래머스 : 코딩테스트연습_스택/큐 (0) | 2022.02.10 |
[C++] 프로그래머스 : 위장 (0) | 2022.01.04 |