๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Swift

[์•Œ๊ณ ๋ฆฌ์ฆ˜/Swift] ์ •๋ ฌ ๋ฌธ์ œํ’€์ด - K๋ฒˆ์งธ์ˆ˜, ๊ฐ€์žฅ ํฐ ์ˆ˜, H-Index

by Danna 2021. 6. 25.
728x90
728x90

Swift ์Šคํ„ฐ๋”” 10์ฃผ์ฐจ 6/24

์Šคํ„ฐ๋”” 4์ฃผ์ฐจ ์ฑ•ํ„ฐ์˜€๋˜ "์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜" ์—์„œ ์‚ฝ์ž… ์ •๋ ฌ, ๋ณ‘ํ•ฉ ์ •๋ ฌ, ํ€ต ์ •๋ ฌ์— ๋Œ€ํ•ด ํ•™์Šตํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ด€๋ จ๋˜์„œ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ์ •๋ ฌ ์ฑ•ํ„ฐ๋ฅผ ํ†ตํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

โœ”๏ธŽ K๋ฒˆ์งธ์ˆ˜

๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

๐Ÿ‘€ ์ž…์ถœ๋ ฅ

 

  • array : Int ๋ฐฐ์—ด, 1 <= ๋ฐฐ์—ด์˜๊ธธ์ด <= 100
  • commands : [i, j, k] ๋ฅผ ์›์†Œ๋กœ ๊ฐ–๋Š” 2์ฐจ์› ๋ฐฐ์—ด, 1 <= ๋ฐฐ์—ด์˜๊ธธ์ด <= 50
  • return : commands ์— ๋งž๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋‹ด์•„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

๐Ÿ’ก ํ’€์ด๋ฐฉ๋ฒ•

 

  • ๊ฐ command ๋งˆ๋‹ค array ๋ฐฐ์—ด์˜ i ~ j ๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ๊ตฌํ•œ๋‹ค. 
  • ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , k๋ฒˆ์งธ ์ˆ˜๋ฅผ ์ •๋‹ต ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ •๋‹ต ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • array ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘, i, j, k ๋Š” ์ธ๋ฑ์Šค๊ฐ€ 1 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์ฐจ์ด๋งŒ ์ž˜ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

 

ํ’€์ด1 - for ๋ฌธ ์ด์šฉ

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    var answer = [Int]()
    for command in commands {
        let i = command[0], j = command[1], k = command[2]
        answer.append(array[i-1...j-1].sorted()[k-1])
    }
    return answer
}

 

ํ’€์ด2 - map ํ•จ์ˆ˜ ์ด์šฉ

 

ํ’€์ด1์—์„œ์˜ command = $0 ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. $0 ์€ ์›์†Œ๊ฐ€ 3๊ฐœ์ธ ์ผ์ฐจ์›๋ฐฐ์—ด์ธ ๊ฒƒ์ด๋‹ค.

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    return commands.map { array[$0[0]-1...$0[1]-1].sorted()[$0[2]-1] }
}

โœ”๏ธŽ ๊ฐ€์žฅ ํฐ ์ˆ˜

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

๐Ÿ‘€ ์ž…์ถœ๋ ฅ

 

  • numbers : ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ 1์ฐจ์› ๋ฐฐ์—ด, 0 <= ๋ฐฐ์—ด์˜ ๊ธธ์ด <= 1,000
  • return : numbers ๋ฐฐ์—ด์˜ ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ’ก ํ’€์ด ๋ฐฉ๋ฒ•

 

  • ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ, ์ด์–ด ๋ถ™์˜€์„ ๋•Œ ๋” ํฐ ์ˆ˜๊ฐ€ ๋˜๋„๋ก ์ •๋ ฌํ•œ๋‹ค.
  • sort(by:) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์˜ ํ•ฉ์œผ๋กœ ์ด์ „๊ฐ’+๋‹ค์Œ๊ฐ’ / ๋‹ค์Œ๊ฐ’+์ด์ „๊ฐ’ ์„ ๋น„๊ตํ•ด ์ •๋ ฌํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค๋ฉด, [33, 30] -> '33'+'30' = '3330' > '30+'33' = '3033' ์ด๋ฏ€๋กœ '33', '30' ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ๋‹ค.
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ :: [0, 0, 0] -> '000' ์œผ๋กœ ์ •๋‹ต์€ 0์ด ๋˜์–ด์•ผํ•˜๋ฏ€๋กœ ๋ณ„๋„๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

 

ํ’€์ด1 - sort, reduce, if-else ํ™œ์šฉ

func solution(_ numbers:[Int]) -> String {

    var stringNumbers: [String] = numbers.map { String($0) }

    stringNumbers.sort { prev, next in
        let first = prev + next
        let second = next + prev
        return first > second
    }

    let answer = stringNumbers.reduce("", +)
    if answer.first! == "0" {
        return "0"
    } else {
        return answer
    }
}

 

ํ’€์ด2 - sorted ์ถ•์•ฝ, joined, ์‚ผํ•ญ์—ฐ์‚ฐ์ž ํ™œ์šฉ

func solution(_ numbers:[Int]) -> String {

    let stringNumbers: [String] = numbers.map { String($0) }
    let answer = stringNumbers.sorted { return $0+$1 > $1+$0 }.joined()
    return answer.first! == "0" ? "0" : answer
}

โœ”๏ธŽ H-Index

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

 

๐Ÿ‘€ ์ž…์ถœ๋ ฅ

 

  • citations : ์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด
  • return : ์ด ๊ณผํ•™์ž์˜ H-Index

 

๐Ÿ’ก ์ฒซ๋ฒˆ์งธ ํ’€์ด

 

  • ๋ฌธ์ œ์— ์จ์ ธ์žˆ๋Š” ๋Œ€๋กœ ํ’€์€ ๋ฐฉ๋ฒ•. (ํ’€์ด2๋ณด๋‹ค ์‹œ๊ฐ„ํšจ์œจ์ด ์•ˆ์ข‹๋‹ค)!
  • h ๋ฅผ 0๋ถ€ํ„ฐ ๋…ผ๋ฌธ์˜ ๊ฐœ์ˆ˜๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์ด hํŽธ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
func solution(_ citations:[Int]) -> Int {

    var hIndex = 0
    while hIndex <= citations.count {
        let count = citations.filter { $0 >= hIndex }.count
        if count < hIndex { break }
        hIndex += 1
    }
    return hIndex - 1
}

 

๐Ÿ’ก ๋‘๋ฒˆ์งธ ํ’€์ด

 

  • ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜ ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. * citations ๋Š” ํ•จ์ˆ˜์ธ์ž๋ผ ์ƒ์ˆ˜์ด๋ฏ€๋กœ sorted ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด return ๊ฐ’์„ ์ด์šฉํ•œ๋‹ค.
  • h ๋ฅผ 0๋ถ€ํ„ฐ ๋…ผ๋ฌธ์˜ ๊ฐœ์ˆ˜๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ h ๋ฒˆ์งธ ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜์™€ ๋น„๊ตํ•œ๋‹ค.
  • ์ฒ˜์Œ์œผ๋กœ ๋‚˜ํƒ€๋‚œ h ๋ฒˆ์งธ ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜๊ฐ€ h ์ด์ƒ์ผ ๋•Œ๊ฐ€ h์˜ ์ตœ๋Œ€๊ฐ’์œผ๋กœ, H-Index ๊ฐ€ ๋˜์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • h ๋ฅผ ์ „๋ถ€ ๋ฐ˜๋ณตํ•œ ๊ฒฝ์šฐ, H-Index ๋Š” ๋…ผ๋ฌธ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™์•„์ง€๊ฒŒ ๋œ๋‹ค.
func solution(_ citations:[Int]) -> Int {
    
    let citations = citations.sorted(by: >)
    for hIndex in 0..<citations.count {
        if hIndex >= citations[hIndex] {
            return hIndex
        }
    }
    return citations.count
}
๋”๋ณด๊ธฐ

ํ’€์ด1๊ณผ ํ’€์ด2 ์ƒ์„ธํ•œ ๊ณผ์ •  (๋”๋ณด๊ธฐ)

citations = [3, 0, 6, 1, 5]
์ •๋ ฌx
h = 0 -> h ์ด์ƒ 5 >= 0
h = 1 -> h ์ด์ƒ 4 >= 1
h = 2 -> h ์ด์ƒ 3 >= 2
h = 3 -> h ์ด์ƒ 3 >= 3
h = 4 -> h ์ด์ƒ 2 >= 4 x --> H-Index = 4-1 = 3

์ •๋ ฌ = [6, 5, 3, 1, 0]
h = 0 -> 0 >= 6 x
h = 1 -> 1 >= 5 x
h = 2 -> 2 >= 3 x
h = 3 -> 3 >= 3 o --> H-Index  = 3

 

๐Ÿค” ํ’€์ด1๊ณผ ํ’€์ด2์˜ ์‹œ๊ฐ„๋ณต์žก๋„ ๋น„๊ต * ์ •๋ ฌ์˜ ํ•„์š”์„ฑ !!

์‹œ๊ฐ„๋ณต์žก๋„ ๋น„๊ต 

 

๐Ÿ“– ์˜ค๋Š˜ ์Šคํ„ฐ๋””์˜ ๋ฆฌ๋ทฐ

  • ๋‹ค๋ฅธ ์Šคํ„ฐ๋””์› ๋ถ„๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ , ๊ฐ™์€ ์–ธ์–ด๋กœ ์ข€ ๋” ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ๊ณ ๋ฏผํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • map, sort, sorted ํ•จ์ˆ˜์˜ closure ๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์ž!
  • [string].joined() ๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ๊นจ์•Œ๋ฌธ๋ฒ• :: array.sort() ๋Š” ๋ฆฌํ„ด๊ฐ’ ์—†์ด array ์š”์†Œ๋ฅผ ์—…๋ฐ์ดํŠธ, array.sorted() ๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ‘€ ์ฐธ๊ณ  ๋งํฌ

 

songda515/SwiftAlgorithm

Swift ๋กœ ํ‘ธ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜. Contribute to songda515/SwiftAlgorithm development by creating an account on GitHub.

github.com

 

728x90
728x90