데이터사이언스 기록기📚

[프로그래머스] Level 2_H-Index(정렬) 본문

Coding Test/프로그래머스(Python)

[프로그래머스] Level 2_H-Index(정렬)

syunze 2022. 9. 22. 01:53

📌문제 유형

정렬

 

📌문제

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

📌나의 문제풀이

1차 시도(87.5)

def solution(citations):
    answer,h = 0,1
    num_index = []
    
    citations.sort(reverse = True)   
    num_index.append(h)
    
    for i in range(1, len(citations)-1):
        h += 1
        num_index.append(h)
        if num_index[i] >= citations[i+1]:
            answer = num_index[i]
            break
            
    return answer

 

2차 시도(통과, +11)

 - num_index : n번 이상 인용된 횟수

 - citations 내림차순 정렬

  • num_index[i]와 citations[i+1]을 비교, citations[i+1]가 num_index[i]이하 인용되었다면 num_index[i]는 h
    (어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면)
  • 인용 횟수가 다 같은 경우 : num_index의 마지막값 리턴
    (인용 횟수가 모두 0일 때, 0 리턴)
def solution(citations):
    answer,h = 0,0
    num_index = [i+1 for i in range(len(citations))]
    
    citations.sort(reverse = True)
    
    # num_index[i]와 citations[i+1]을 비교, num_index[i] 값이 크면 answer로 리턴
    for i in range(0, len(citations)-1):
        if num_index[i] >= citations[i+1]:
            answer = num_index[i]
            break
            
   	# [5,5,5] return 3인 경우
    if citations[-1] >= num_index[-1]:
        return num_index[-1]
    # [0,0,0] return 0인 경우
    elif citations[0] == 0:
        return 0
            
    return answer

 

📌다른사람의 문제 풀이

1) 

1) min(index,value) 부분은 가능할 수 있는 모든 h-index를 추출하는 부분
2) max(~) 값은 가능할 수 있는 모든 h-index 중 가장 큰 값을 추출하는 부분.
예를들어 [6, 5, 4, 1, 0]의 경우에선 min~ 부분은 min(1, 6), min(2, 5), min(3, 4), min(4, 1), min(5, 0), 즉 해당 인용수 이상의 논문개수와 해당 논문의 인용수 중 더 작은 숫자를 고르는 작업을 하고(h-index로 가능한 숫자 추출), max~부분은 앞에서 골라진 (1, 2, 3, 1, 0) 중 가장 큰 숫자를 뽑아 실제 h-index를 구하는 방법

출처 : https://school.programmers.co.kr/learn/courses/30/lessons/42747/solution_groups?language=python3
def solution(citations):
    citations.sort(reverse=True)
    answer = max(map(min, enumerate(citations, start=1)))
    return answer

 

2)

def solution(citations):
    citations = sorted(citations)
    l = len(citations)
    for i in range(l):
        if citations[i] >= l-i:
            return l-i
    return 0

 

📌리뷰

 - 문제 제대로 이해하기

 - 이외의 경우의 수 생각해보기

728x90
Comments