Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • boj
  • 다이나믹 프로그래밍
  • BFS
  • docker
  • django
  • 백트래킹
  • 프로그래머스
  • Kubernetes
  • Network
  • DFS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/프로그래머스

[프로그래머스 level2] H-index (C++)

2022. 2. 22. 17:20
어떤 과학자가 발표한 논문 
n편 중, 
h번 이상 인용된 논문이 
h편 이상이고 나머지 논문이 h번 이하 인용되었다면 
h의 최댓값이 이 과학자의 H-Index입니다.

위 말을 이해하기가 어려울 수 있다.

h번 이상 인용된 논문이 h편 이상이다 -> 이 문장의 뜻은 h는 과학자가 발표한 논문의 개수 이하가 될 것이다.

 

예시를 보면서 이해해 보자.

논문의 인용 횟수 [3, 0, 6, 1, 5] 가 주어진다.

 

먼저 논문을 오름차순 정렬한다.

[0, 1, 3, 5, 6] 

 

여기서 2회 이상 인용된 논문은 3, 5, 6 논문이 되고, 2회 이하 인용된 논문은 0, 1 논문이 된다.

3회 이상 인용된 논문은 3, 5, 6 논문이 되고, 3회 이하 인용된 논문은 0, 1, 3 논문이 된다.

 

배열이 정렬 되었기 때문에 lower_bound와 upper_bound를 사용하면 인용 횟수가 i번 이상인 논문의 개수를 알 수 있다.

 

인용 횟수가 i번 이상인 논문 up

int up = citations.size() - (lower_bound(citations.begin(), citations.end(), i) - citations.begin());

 

인용 횟수가 i번 이하인 논문 down

int down = upper_bound(citations.begin(), citations.end(), i) - citations.begin();

 

실제 답을 구하는 조건문

if(up >= i && down <= i){
    answer = i;
}

 

up회 이상 인용된 논문의 개수는 i 이상이어야 한다 == h번 이상 인용된 논문이 h편 이상이어야 한다.

 

 

 

 

최종 코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> citations) {
    int answer = 0;
    sort(citations.begin(), citations.end());
    
    for(int i = 0; i <= citations.size(); i++){
        int up = citations.size() - (lower_bound(citations.begin(), citations.end(), i) - citations.begin());
        int down = upper_bound(citations.begin(), citations.end(), i) - citations.begin();
        if(up >= i && down <= i){
            answer = i;
        }
    }
    
    
    return answer;
}

 

 

 

 

 

 

 

 

'Algorithm > 프로그래머스' 카테고리의 다른 글

[프로그래머스 level2] 프렌즈4블록 (C++)  (0) 2022.02.22
[프로그래머스 level2] 피로도 (C++)  (0) 2022.02.22
[프로그래머스] 다리를 지나는 트럭(C++) level2  (0) 2022.01.27
[프로그래머스] level2 순위 검색 (Python)  (0) 2022.01.26
[프로그래머스] level2 전화번호 목록 (C++)  (0) 2022.01.18
    'Algorithm/프로그래머스' 카테고리의 다른 글
    • [프로그래머스 level2] 프렌즈4블록 (C++)
    • [프로그래머스 level2] 피로도 (C++)
    • [프로그래머스] 다리를 지나는 트럭(C++) level2
    • [프로그래머스] level2 순위 검색 (Python)

    티스토리툴바