CK 011 | H-Index (python)

This Is Empty.ยท2021๋…„ 9์›” 5์ผ
0

codekata

๋ชฉ๋ก ๋ณด๊ธฐ
11/35
post-thumbnail

๋ฌธ์ œ

๐Ÿ”— ์ •๋ ฌ | H-Index

H-Index๋Š” ๊ณผํ•™์ž์˜ ์ƒ์‚ฐ์„ฑ๊ณผ ์˜ํ–ฅ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์–ด๋Š ๊ณผํ•™์ž์˜ H-Index๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ธ h๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋ฐฑ๊ณผ1์— ๋”ฐ๋ฅด๋ฉด, H-Index๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ nํŽธ ์ค‘, h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์ด hํŽธ ์ด์ƒ์ด๊ณ  ๋‚˜๋จธ์ง€ ๋…ผ๋ฌธ์ด h๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋˜์—ˆ๋‹ค๋ฉด h์˜ ์ตœ๋Œ“๊ฐ’์ด ์ด ๊ณผํ•™์ž์˜ H-Index์ž…๋‹ˆ๋‹ค.

์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด citations๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๊ณผํ•™์ž์˜ H-Index๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์˜ ์ˆ˜๋Š” 1ํŽธ ์ด์ƒ 1,000ํŽธ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋…ผ๋ฌธ๋ณ„ ์ธ์šฉ ํšŸ์ˆ˜๋Š” 0ํšŒ ์ด์ƒ 10,000ํšŒ ์ดํ•˜์ž…๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

def solution(citations):
    citations.sort()
    for h in range(max(citations), 0, -1):
        overh = 0

        for citation in citations:
            if citation >= h:
                overh += 1

            if overh == h:
                return h
    return 0

๋‹ค์Œ์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ ํ’€์ดํ–ˆ๋‹ค.

  1. citations๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์ค€๋‹ค.
citations.sort()

์‚ฌ์‹ค ๋ฐฐ์—ด์„ ์ •๋ ฌ์„ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ํ†ต๊ณผ๋œ๋‹ค. ์‹คํ–‰์‹œ๊ฐ„์ด ์•ฝ๊ฐ„ ๋‚ด๋ ค๊ฐ€์ง€๋งŒ ๋ฏธ๋ฏธํ•˜๋‹ค. (์‹œ๊ฐ„๋ณต์žก๋„๋„ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ๊ฒ ๋‹ค..)

  1. max(citations)๋ถ€ํ„ฐ 0๊นŒ์ง€ -1ํ•˜๋ฉด์„œ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ฐ ๋…ผ๋ฌธ๋“ค์ด hํŽธ ์ด์ƒ ์ธ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ์œ„ํ•ด overh๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.
for h in range(max(citations), 0, -1):
        overh = 0
  1. citations๋ฐฐ์—ด ์•ˆ์—์žˆ๋Š” citation๋งˆ๋‹ค citation์ด hํŽธ ์ด์ƒ ์ธ์šฉ๋˜์—ˆ๋‹ค๋ฉด overh += 1์„ ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  overh๊ฐ€ h ๊ฐ’๊ณผ ๊ฐ™์•„์กŒ๋‹ค๋ฉด h๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
...
	for citation in citations:
            if citation >= h:
                overh += 1

            if overh == h:
                return h
	return 0

์ด๋•Œ, ๋งˆ์ง€๋ง‰ ์ค„์— return 0๊ฐ€ ์—†๋‹ค๋ฉด ํ…Œ์ŠคํŠธ 16์ด ํ†ต๊ณผ๋˜์ง€ ์•Š๋Š”๋‹ค. citations์ด [0,0,0]์ฒ˜๋Ÿผ 0๋งŒ ๋‹ด๊ฒจ์žˆ๋‹ค๋ฉด 2๋ฒˆ์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์•„ null์ด ๋ฆฌํ„ด๋œ๋‹ค. ์ด ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ์œ„ํ•ดreturn 0๋ฅผ ์ถ”๊ฐ€ํ•ด ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.


์–ด๋งˆ์–ด๋งˆํ•œ.. ์‹œ๊ฐ„๋ณต์žก๋„........

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

def solution(citations):
    citations.sort(reverse=True)
    answer = max(map(min, enumerate(citations, start=1)))
    return answer
profile
Convinced myself, I seek not to convince.

0๊ฐœ์˜ ๋Œ“๊ธ€