๐ ์ ๋ ฌ | H-Index
H-Index๋ ๊ณผํ์์ ์์ฐ์ฑ๊ณผ ์ํฅ๋ ฅ์ ๋ํ๋ด๋ ์งํ์ ๋๋ค. ์ด๋ ๊ณผํ์์ H-Index๋ฅผ ๋ํ๋ด๋ ๊ฐ์ธ h๋ฅผ ๊ตฌํ๋ ค๊ณ ํฉ๋๋ค. ์ํค๋ฐฑ๊ณผ1์ ๋ฐ๋ฅด๋ฉด, H-Index๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํฉ๋๋ค.
์ด๋ค ๊ณผํ์๊ฐ ๋ฐํํ ๋ ผ๋ฌธ nํธ ์ค, h๋ฒ ์ด์ ์ธ์ฉ๋ ๋ ผ๋ฌธ์ด hํธ ์ด์์ด๊ณ ๋๋จธ์ง ๋ ผ๋ฌธ์ด h๋ฒ ์ดํ ์ธ์ฉ๋์๋ค๋ฉด h์ ์ต๋๊ฐ์ด ์ด ๊ณผํ์์ H-Index์ ๋๋ค.
์ด๋ค ๊ณผํ์๊ฐ ๋ฐํํ ๋ ผ๋ฌธ์ ์ธ์ฉ ํ์๋ฅผ ๋ด์ ๋ฐฐ์ด citations๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ด ๊ณผํ์์ H-Index๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
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
๋ค์์ ์์์ ๋ฐ๋ผ ํ์ดํ๋ค.
citations
๋ฐฐ์ด์ ์ ๋ ฌํด์ค๋ค.citations.sort()
์ฌ์ค ๋ฐฐ์ด์ ์ ๋ ฌ์ ํด์ฃผ์ง ์์๋ ํต๊ณผ๋๋ค. ์คํ์๊ฐ์ด ์ฝ๊ฐ ๋ด๋ ค๊ฐ์ง๋ง ๋ฏธ๋ฏธํ๋ค. (์๊ฐ๋ณต์ก๋๋ ๊ณต๋ถ๋ฅผ ํด์ผ๊ฒ ๋ค..)
max(citations)
๋ถํฐ 0๊น์ง -1ํ๋ฉด์ ๊ฒ์ฌํ๋ค. ๊ฐ ๋
ผ๋ฌธ๋ค์ด hํธ ์ด์ ์ธ์ฉ๋์๋์ง ํ์ธํ๊ธฐ์ํด overh
๋ฅผ 0์ผ๋ก ์ด๊ธฐํํด์ค๋ค.for h in range(max(citations), 0, -1):
overh = 0
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