Focus On Develop ๐ŸคŸ๐ŸคŸ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] H-Index ๋ณธ๋ฌธ

iOS [Swift]/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] H-Index

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2020. 12. 28. 21:06

<๋ฌธ์ œ>


<์ „๋žต์งœ๊ธฐ>

์Œ.. ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ์ฒซ๋ฒˆ์งธ๋Š” Brute Force ๋ฐฉ์‹์œผ๋กœ, H-Index๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ citations๋ฅผ ํ™•์ธํ•˜๋Š”๋ฐ, H-Index์— ํ•ด๋‹น๋˜๋Š” ์ˆ˜๊ฐ€ citations ์•ˆ์— H-Index๊ฐœ ์ด์ƒ์žˆ๋Š”์ง€?๋ฅผ ๋ณด๋‹ค๊ฐ€.. ์•„๋‹Œ ์ˆœ๊ฐ„์ด ์˜ค๋ฉด break ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. (๊ทผ๋ฐ ์ด๋ฐฉ๋ฒ•์€.. ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ๊ณผ ์žˆ์–ด์„œ๋Š” ์•ˆ๋  ์ผ€์ด์Šค๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด 1000์œผ๋กœ 1000๊ฐœ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค๋ฉด..?ใ…‹ใ…‹ ์‰ฃ..๐Ÿ‘Š) ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋˜ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ citations ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์™€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ์‹œ๋ฅผ ์˜ˆ๋กœ๋“ค๋ฉด, ์ •๋ ฌ๋œ citations๋Š” 0, 1, 3, 5, 6 ์ด๊ณ , totalCount - index -1 ๊ณผ ๋น„๊ตํ•ด์„œ citations๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด.. ์ ์–ด๋„ totalCount-index-1๋ณด๋‹ค ์ธ์šฉ๋œ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ๋…ผ๋ฌธ์ด totalCount-index-1๊ฐœ์ด์ƒ ์žˆ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค. ์ด๋•Œ totalCount-index-1์ด ์ตœ๋Œ€๊ฐ€ ๊ตฌํ•˜๋Š” ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. 

 

<์ฝ”๋“œ>

import Foundation

func solution(_ citations:[Int]) -> Int {
    var totalCnt = citations.count
    var HIndex = 0
    
    if citations.reduce(0, +) == 0 {
        return 0
    }
    
    for (i, x) in citations.sorted().enumerated() {
        if totalCnt-i-1 >= x {
            HIndex = totalCnt-i-1
        } else {
            break
        }
    }
    
    if HIndex == 0 {
        HIndex = totalCnt
    }
    
    return HIndex
}

 

<Lesson Learned>

์ „๋žต ์ง ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋”๋‹ˆ ์˜ˆ์™ธ์ผ€์ด์Šค๊ฐ€ 2๊ฐ€์ง€ ์žˆ์—ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ [5,5,5,5,5] -> H-Index๋Š” 5์—ฌ์•ผ ํ•˜๋Š”๋ฐ ๊ฒฐ๊ณผ๋Š” 0์ด ๋‚˜์˜ค๊ณ , ์ด ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰์— H-Index๊ฐ€ 0์ด๋ฉด citations์˜ ์นด์šดํŠธ๋ฅผ ๋„ฃ๋„๋ก ํ–ˆ๋Š”๋ฐ.. [0,0,0] -> ์ด ์ผ€์ด์Šค์—๋Š” H-Index๊ฐ€ 0์ด์—ฌ์•ผ ํ•˜๋Š”๋ฐ 3์ด ๋˜๋Š” ์˜ˆ์™ธ๊ฐ€ ์žˆ์–ด์„œ.. ์‹œ์ž‘ํ•˜๊ธฐ์ „์— ์ „์ฒด ํ•ฉ์ด 0์ด๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๋‹ค.. ใ…œใ…œ ๐Ÿ˜ญ ๊ทธ๋Ÿฐ๋ฐ ๋ง์ž…๋‹ˆ๋‹ค.. ์ด ๋ชจ๋“ ๊ฒŒ citations๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค....๐Ÿ˜ญ ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ ์•„๋‹ˆ... ์˜ค๋ฆ„์ฐจ์ˆœํ•ด์„œ ๋ฌธ์ œ ํ‘ผ์‚ฌ๋žŒ ๋‚˜๋ฐ–์— ์—†์ž–์•„..??๐Ÿ‘Š๐Ÿ‘Š ใ…œใ…œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์œผ๋ฉด ์ง„์งœ ํ›จ์”ฌ ๊ฐ„๋‹จํ–ˆ์„ํ…๋ฐ.. ใ…‹ใ…‹ใ…‹ใ…‹ ์˜ค๋Š˜์˜ ๊ตํ›ˆ : ์ •๋ ฌํ• ๋•Œ๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ, ์˜ค๋ฆ„์ฐจ์ˆœ ์–ด๋–ป๊ฒŒ ์ •๋ ฌํ• ์ง€ ๊ผญ ์ƒ๊ฐํ•˜๊ณ  ์‹œ์ž‘ํ•˜์ž ํ‘ธํ•˜ํ•˜....๐Ÿ˜‚๐Ÿ˜‚

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉ๋œ reduce๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ??  ๐Ÿ‘‰ 2020/12/15 - [iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!] - [Swift] map, filter, reduce

 

 

์ถœ์ € : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค H-Index

Comments