[프로그래머스] 2021 카카오 채용연계형 인턴십 > 표 편집 (81303)

원본 문제 보기

문제 설명

원본 문제 보기

[입출력 예]

n k cmd result
8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z"] "OOOOXOOO"
8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"] "OOXOXOOO"

문제 풀이 (KOTLIN)

정확성 : 30
효율성 : 35.0
합계 : 65.0 / 100

import java.lang.StringBuilder
import java.util.*

class Solution {
    var check = mutableSetOf<Int>()

    fun solution(n: Int, k: Int, cmd: Array<String>): String {
        val sb = StringBuilder()
        val deleted = Stack<Int>()
        var cursor = k

        cmd.forEach {
            when (it) {
                "C" -> {
                    check.add(cursor)
                    deleted.push(cursor)
                    val new = findNewIndex(n, cursor, -1, 0)
                    cursor = if (new >= n) {
                        findNewIndex(n, cursor, 1, 0)
                    } else {
                        new
                    }
                }
                "Z" -> {
                    val prev = deleted.pop()
                    check.remove(prev)
                }
                else -> {
                    val parsedCmd = it.split(" ")
                    cursor = when (parsedCmd[0]) {
                        "D" -> {
                            findNewIndex(n, cursor, -1, parsedCmd[1].toInt())
                        }
                        else -> {
                            findNewIndex(n, cursor, 1, parsedCmd[1].toInt())
                        }
                    }
                }
            }
        }


        if (deleted.isEmpty()) {
            repeat(n) {
                sb.append('O')
            }
        } else {
            repeat(n) {
                sb.append(if (it in check) 'X' else 'O')
            }
        }

        return sb.toString()
    }

    fun findNewIndex(n: Int, index: Int, di: Int, jump: Int): Int {
        var newIndex = 0
        var count = 0
        when  {
            di == 0 -> {
                newIndex = index
            }
            di < 0 -> {
                for (i in index..n) {
                    if (i !in check) {
                        newIndex = i
                        if (count == jump) {
                            break
                        } else {
                            count++
                        }
                    }
                }
            }
            di > 0 -> {
                for (i in index downTo 0) {
                    if (i !in check) {
                        newIndex = i
                        if (count == jump) {
                            break
                        } else {
                            count++
                        }
                    }
                }
            }
        }

        return newIndex
    }
}

문제 풀이 (KOTLIN)

풀이 참고 정확성 : 30
효율성 : 70
합계 : 100 / 100

import java.util.Stack

class Solution {
    fun solution(n: Int, k: Int, cmd: Array<String>): String {
        val stack = Stack<Int>()
        var index = k
        var size = n

        for (query in cmd) {
            val key = query[0]

            when (key) {
                'U' -> { index -= query.substring(2).toInt() }
                'D' -> { index += query.substring(2).toInt() }
                'C' -> {
                    stack.push(index)
                    size--
                    if (index == size) index--
                }
                'Z' -> {
                    val undo = stack.pop()
                    if (undo <= index) index++
                    size++
                }
            }
        }

        val sb = StringBuilder()
        for (i in 0 until size) {
            sb.append('O')
        }
        while (!stack.isEmpty()) {
            sb.insert(stack.pop().toInt(), 'X')
        }
        return sb.toString()
    }
}

문제 풀이 (SWIFT)

풀이 참고 정확성 : 30
효율성 : 35
합계 : 65 / 100

import Foundation

func solution(_ n:Int, _ k:Int, _ cmd:[String]) -> String {
    var list: [(Int, Bool)] = [(index: Int, isDeleted: Bool)](repeating: (0, false), count: n)
    
    for i in 0..<n {
        list[i].0 = i
    }
    
    return operation(&list, cmd, n, k)
}

func operation(_ list: inout [(Int, Bool)], _ cmd: [String], _ n: Int, _ k: Int) -> String {
    func down(_ count: Int) {
        var step = count
        for i in current + 1..<list.count {
            if step == 0 { break }
            if !list[i].1 {
                step -= 1
            }
            current = i
        }
    }
    
    func up(_ count: Int) {
        var step = count
        for i in stride(from: current - 1, through: 0, by: -1) {
            if step == 0 { break }
            if !list[i].1 {
                step -= 1
            }
            current = i
        }
    }
    
    func deletion() {
        func findCurrent() {
            var newCurrent: Int? = nil
            
            for i in current..<list.count {
                if !list[i].1 {
                    newCurrent = i
                    break
                }
            }
            
            if newCurrent == nil {
                for i in stride(from: current, through: 0, by: -1) {
                    if !list[i].1 {
                        current = i
                        break
                    }
                    
                }
            } else {
                current = newCurrent!
            }
        }
        
        stack.append(current)
        list[current].1 = true
        findCurrent()
    }
    
    func undo() {
        list[stack.removeLast()].1 = false
    }
    
    func printResult() -> String {
        for i in stack {
            str[i] = "X"
        }
        
        return str.joined(separator: "")
    }
    
    var current = k
    var stack = [Int]()
    var str = [String](repeating: "O", count: n)
    
    for c in cmd {
            let op = c.split(separator: " ").map { String($0) }
            if op.count == 1 {
                switch op[0]
                {
                case "Z":
                    undo()
                default:
                    deletion()
                }
            } else {
                switch op[0]
                {
                case "D":
                    down(Int(op[1])!)
                default:
                    up(Int(op[1])!)
                }
            }
        }
    
    return printResult()
}