[정렬] H-Index (Level 2)

정은경·2020년 4월 18일
0

1. 문제

2. 나의 풀이

def solution(citations):
    answer = len(citations) # answer의 최대값이 될 수 있는 최대값은 citation의 개수!
    citations.sort()
    
    while True:
        cnt = 0
        for value in citations:
            if value >= answer:
                cnt += 1
            if answer <= cnt:
                return answer
        answer -= 1 # 최대값을 1씩 줄여가면서 매칭되는 것을 찾는 것!

3-1. 남의 풀이

def solution(citations):
    answer = len(citations)
    citations.sort()

    while 1:
        cnt =0
        for value in citations:
            if value >= answer:
                cnt+=1
            if answer <= cnt:
                return answer
        answer-=1

    return answer

3-2. 남의 풀이

def solution(citations):
    citations = sorted(citations)
    print(citations)
    l = len(citations)
    for i in range(l):
        print(l-i)  # l-i의 의미가 뭘까?
        if citations[i] >= l-i:
            return l-i
    return 0


citations = [3, 0, 6, 1, 5]

print(solution(citations))
  • print(l-i) # l-i의 의미가 뭘까?
  • 전체개수에서 한개씩 감소한다는 의미
  • 첫번째 최댓값은 citation의 길이
  • 두번째 최댓값은 citation의 길이 -1
  • 세번째 최댓값은 citation의 길이 -2
  • 오름차순으로 정렬된 citation을 하나씩 보면서
  • n번째 수가 (citation-n번째)와 같거나 크다면
    "(citation-n번째)"를 리턴

4. 느낀 점

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글