๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€2 110 ์˜ฎ๊ธฐ๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 10. 27.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 110 ์˜ฎ๊ธฐ๊ธฐ

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ์–ด๋–ค ๋ฌธ์ž์—ด x์— ๋Œ€ํ•ด์„œ, ๋‹น์‹ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ–‰๋™์„ ํ†ตํ•ด x๋ฅผ ์ตœ๋Œ€ํ•œ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์•ž์— ์˜ค๋„๋ก ๋งŒ๋“ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. x์— ์žˆ๋Š” "110"์„ ๋ฝ‘์•„์„œ, ์ž„์˜์˜ ์œ„์น˜์— ๋‹ค์‹œ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ

programmers.co.kr


Solution

 

์ด๋ฒˆ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ์Šคํƒ์„ ์ด์šฉํ•ด์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

1. ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  "110" ์‚ญ์ œํ•ด์ค€๋‹ค.

 

์—ฌ๊ธฐ์„œ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด 4๋ฒˆ,18๋ฒˆ,19๋ฒˆ,21๋ฒˆ ์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ž ํ•˜๋‚˜์‹ ์Œ“์œผ๋ฉฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋‘ ๊ธ€์ž์™€ ํ˜„์žฌ ๊ธ€์ž๋ฅผ ๋น„๊ตํ•ด์„œ "110" ์ด๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด

 

์Šคํƒ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋‘ ๋ฌธ์ž๋ฅผ ์ง€์›Œ์ค๋‹ˆ๋‹ค.

 

"110"์„ ์ง€์› ์„ ๋•Œ ๊ทธ ์ด์ „์— ์žˆ๋˜ ๋ฌธ์ž์™€ ์ดํ›„์— ์žˆ๋˜ ๋ฌธ์ž๊ฐ€ ํ•ฉ์ณ์ ธ์„œ

 

๋˜ "110"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์Šคํƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

ex) 111111100 -> 111110 -> 111

 

๊ทธ๋ฆฌ๊ณ  ๋‚˜์ค‘์— 110์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋ฏ€๋กœ ๋”ํ•ด์„œ ๋”ฐ๋กœ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

func remove110(str:String,add:inout String) -> [String] {
    var stack:[String] = []
    for s in str {
        if stack.count >= 2 && stack[stack.count-2] == "1" && stack[stack.count-1] == "1" && s == "0" {
            stack.removeLast()
            stack.removeLast()
            add += "110"
            continue
        }
        stack.append(String(s))
    }
    return stack
}

 

2. 1์—์„œ "110"์„ ์ง€์šด ๋ฌธ์ž์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ "0"์„ ์ฐพ์•„๋‚ธ๋‹ค.

 

์‚ฌ์ „์— ๊ฐ€์žฅ ์•ž์— ์˜ค๊ธฐ ์œ„ํ•œ ๊ทœ์น™์€ ๋ชจ๋“  "110"์„ ์ œ๊ฑฐํ•œ ๋ฌธ์ž์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ "0"์— "110"์„ ๋”ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๊ณ ๋กœ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ "0"์ด ์žˆ๋Š” index๋ฅผ ์ฐพ์•„์ค๋‹ˆ๋‹ค.

 

func findLast0(str:[String]) -> Int {
    for i in stride(from: str.count-1, through: 0, by: -1) {
        if str[i] == "0" {
            return i+1
        }
    }
    return 0
}

 

3. "110"์„ ์ง€์›Œ์ค€ ๋ฌธ์ž์—ด์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ "0"์ด ์žˆ๋Š” ์ธ๋ฑ์Šค์— "110"๋“ค์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

"110"์„ ์ง€์šธ ๋•Œ ์ €์žฅํ•ด๋†“์€ add๋ฅผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ "0"์ด ์žˆ๋Š” index์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

func insert110ToRemoveString(str:String) -> String {
    var add:String = ""
    var removeString = remove110(str: str,add: &add)
    let index = findLast0(str: removeString)
    removeString.insert(add, at: index)
    return removeString.joined()
}

 

4. s์— ๋‹ด๊ธด ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋ฐ˜๋ณตํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

func solution(_ s:[String]) -> [String] {
    return s.map{insert110ToRemoveString(str: $0)}
}

Source Code

 


P.S

 

"110"์„ ์ง€์šฐ๋Š” ๊ณผ์ •์—์„œ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  while๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ 110์„ ์ง€์šฐ๊ณ  break๋ฅผ ๊ฑธ๊ณ 

 

๋‹ค์‹œ ์ง€์šฐ๊ณ  ํ•˜๋ฉด์„œ "110"์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ–ˆ๋”๋‹ˆ 8๋ฒˆ,19๋ฒˆ,21๋ฒˆ์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค...

 

ํ’€์ด๋ฒ•์— ์Šคํƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ... ์ •๋ง ํ•œ ๋— ์ฐจ์ด๋กœ ์ •๋‹ต์ธ์ง€ ์•„๋‹Œ์ง€๊ฐ€ ๊ฐˆ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™๋‹ค...

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€