๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Coding Test

[Level 2] H-Index (Python)

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - H-Index

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

programmers.co.kr

 

def solution(c):
    candi_h = []
    
    if not max(c):
        return 0
    
    for h in range(1, max(c)+1):
        l_h = 0
        u_h = 0
        
        for i in range(len(c)):
            if c[i] >= h:
                u_h += 1
            if c[i] <= h:
                l_h += 1
            
        if (u_h >= h) and (l_h <= h):
            candi_h.append(h)

    return(max(candi_h))

 

โ–ท ๋ฌธ์ œ์˜ ๋…ผ๋ฆฌ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ํ’€์—ˆ๋‹ค.

 

โ–ถ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ์˜ ๋…ผ๋ฆฌ๋ฅผ ํ•œ ์ค„๋กœ ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

def solution(c):
    c.sort(reverse = True)
    
    h = max(map(min, enumerate(c, start = 1)))
    
    return h