
프로그래머스 - 정렬 - [Lv.2] H-Index
[풀이 기록]
????/??/?? - Java 풀이 완료
2022/07/19 - C++ 풀이 완료
[작성 코드]
- Java
더보기
import java.util.*;
class Solution {
public int solution(int[] citations) {
int answer = 0;
Arrays.sort(citations);
for(int i=0; i<citations.length;i++){
int h = citations.length - i;
if(citations[i] >= h){
answer = h;
break;
}
}
return answer;
}
}
✔️정렬 후 조건에 맞는 h 찾기
- C++
더보기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> citations) {
int answer = 0;
int h=0, p=0;
sort(citations.begin(), citations.end(), greater<>());
for(h=citations[0]; h>=0; h--){
if(find(citations.begin(), citations.end(), h) != citations.end()){
p = find(citations.begin(), citations.end(), h) - citations.begin()+1;
}
if(h <= p){
break;
}
}
if(citations.back() > h) h = citations.size();
answer = h;
return answer;
}
✔️정렬 후 조건에 맞는 h 찾기
✔️[100, 100, 100] 등의 경우에 h가 citations의 크기가 되어야 함
❌find를 이용해 p를 세팅하지 않고도 citations의 index를 이용해 h값을 찾을 수 있음
'📝코딩테스트' 카테고리의 다른 글
| 올바른 괄호 (0) | 2022.07.19 |
|---|---|
| K번째수 (0) | 2022.07.19 |
| 가장 큰 수 (0) | 2022.07.18 |
| 프린터 (0) | 2022.07.18 |
| 더 맵게 (0) | 2022.07.18 |