ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm

[Algorithm] ๋ฌธ์ž์—ด ์••์ถ•

_๋ณด๋ฆ„ 2021. 4. 16. 15:19

๐Ÿ–ฅ ๋ฌธ์ œ

์••์ถ•ํ•  ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์œ„์— ์„ค๋ช…ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ 1๊ฐœ ์ด์ƒ ๋‹จ์œ„๋กœ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•œ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

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

  1. ๋ณ€์ˆ˜
    • s: [String] : ์••์ถ•ํ•  ๋ฌธ์ž์—ด
    • compressedS: [String] : ์••์ถ•๋œ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž์—ด
    • start: Int : ๋น„๊ต์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค
    • current: Int : ๋น„๊ต์ค‘์ธ ์ธ๋ฑ์Šค
    • num: Int : ์••์ถ•์ด ๊ฐ€๋Šฅํ•œ ๊ฐœ์ˆ˜
    • tmp: [string] : ์••์ถ•๋œ ๋ฌธ์ž์—ด
  2. for i in 1...s.count/2 + 1 {}
    • 1๊ฐœ ~ s.count/2๊ฐœ์˜ ๋ฌธ์ž์—ด ๋‹จ์œ„๋กœ ์••์ถ•ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ฐ˜๋ณตํ•œ๋‹ค.
  3. while current+i-1 < s.count && s[start...start+i-1] == s[current...current+i-1] {}
    • ๋น„๊ต์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฌธ์ž์—ด๊ณผ ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•œ๋‹ค.
    • ๋ฌธ์ž์—ด์ด ์—ฐ์†๋˜๋ฉด ์—ฐ์†๋˜๋Š” ํšŸ์ˆ˜๋งŒํผ num์„ +1 ํ•ด์ค€๋‹ค.
  4. 2๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋  ๊ฒฝ์šฐ, ์ฆ‰ num์ด 2 ์ด์ƒ์ผ ๊ฒฝ์šฐ
    • tmp.append(String(num))
    • tmp.append(contentsOf: s[start...start+i-1])
    • num๊ณผ ๊ธฐ์ค€ ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด ์••์ถ•ํ•œ๋‹ค.
  5. tmp.append(contentsOf: s[start...start+i-1])
    • ์••์ถ•์ด ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ธฐ์ค€ ๋ฌธ์ž์—ด๋งŒ ๋„ฃ๋Š”๋‹ค.
  6. ๋ชจ๋“  ๋น„๊ต๊ฐ€ ๋๋‚˜๊ณ  ๋‚จ์€ ๋ฌธ์ž์—ด์„ ๋„ฃ๋Š”๋‹ค.
  1. compressedS = tmp == [] || compressedS.count < tmp.count ? compressedS : tmp
    • compressedS์™€ tmp๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž์—ด์„ compressedS์— ๋„ฃ๋Š”๋‹ค.
  2. return compressedS.map{$0}.reduce("", +).count
    • ๋ชจ๋“  ๊ณผ์ •์ด ๋๋‚˜๋ฉด compressedS์˜ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Swift Code

import Foundation

func solution(_ s:String) -> Int {
    let s = s.map{String($0)}
    var compressedS = s

    // 1 ~ s.count/2 ๊ฐœ์˜ ๋ฌธ์ž์—ด ๋‹จ์œ„
    for i in 1...s.count/2 + 1 {
        var start = 0
        var current = i
        var num = 1
        var tmp: [String] = []

        while current < s.count {
            // i๊ฐœ์˜ ๋ฌธ์ž์—ด ๋‹จ์œ„๋กœ ๋น„๊ต
            while current+i-1 < s.count && s[start...start+i-1] == s[current...current+i-1] {
                current += i
                num += 1
            }

            // ์••์ถ•, 2์ด์ƒ ๋ฐ˜๋ณต๋˜๋ฉด ๋ฐ˜๋ณต๋œ ํšŸ์ˆ˜ ์ถ”๊ฐ€
            if num > 1 {
                tmp.append(String(num))
                tmp.append(contentsOf: s[start...start+i-1])
            } else {
                tmp.append(contentsOf: s[start...start+i-1])
            }

            // ์••์ถ•๋˜์ง€ ์•Š์€ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€
            if current >= s.count - i && current < s.count {
                while current < s.count {
                    tmp.append(s[current])
                    current += 1
                }
            }
            start = current
            current += i
            num = 1
        }
        compressedS = tmp == [] || compressedS.count < tmp.count ? compressedS : tmp
    }
    return compressedS.map{$0}.reduce("", +).count
}

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ์••์ถ•

๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ "์–ดํ”ผ์น˜"๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ

programmers.co.kr

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ