IS0
0
IS0
전체 방문자
오늘
어제
  • 분류 전체보기
    • 🎲게임개발
      • 🔫팡팡 바이러스
      • 🐍기생충
      • 🐦갓버드
    • 💻기타 프로젝트
      • 🚛BARO_AR Ruler
    • 📝코딩테스트
    • 🤖강화학습

블로그 메뉴

  • 홈
  • 태그
  • 관리

공지사항

인기 글

태그

  • c++
  • oculus
  • 힙
  • 정렬
  • ARcore
  • AR Foundation
  • 큐
  • 스택
  • 해시
  • PYTHON
  • Unity
  • AR
  • FPS
  • VR
  • java

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
IS0

0

H-Index
📝코딩테스트

H-Index

2022. 7. 19. 17:58

프로그래머스 - 정렬 - [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
    IS0
    IS0

    티스토리툴바