๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ–ฅ CS/Algorithm

[Algorithm/Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Lv.3) - ํ‘œ ํŽธ์ง‘

์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค :)

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๊ฒŒ ๋˜์–ด ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š”๋ฐ์š”,

<ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Lv.3) - ํ‘œ ํŽธ์ง‘> ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๋ฉฐ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ(LinkedList)๋ฅผ Swift๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

๋ฌธ์ œ ๋งํฌ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๋ฌธ์ œ ํ’€์ด

์ฒ˜์Œ ์ ‘๊ทผ

์ €๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์จ์•ผ๊ฒ ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ•ด ํ•œ ๋ฒˆ์— ๋งž์ถ”์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค,,

์„ ํƒ๋œ ํ–‰์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ๋ช…๋ น์–ด ์ผ€์ด์Šค์— ๋”ฐ๋ผ ์•Œ๋งž์€ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก(ํฌ์ธํ„ฐ ์ด๋™, ์‚ญ์ œ๋œ ํ–‰ ์ธ๋ฑ์Šค ๊ธฐ๋ก ๋“ฑ) ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ด๋ฅผ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‚ฝ์ž…, ์‚ญ์ œ ์—ฐ์‚ฐ ์—†์ด ๋‹จ์ˆœํžˆ ํฌ์ธํ„ฐ๋งŒ์œผ๋กœ ๋ช…๋ น์–ด์— ๋”ฐ๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ณ€ํ™”๋ฅผ ๊ธฐ๋กํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๋‹ค ๋”ฐ์ ธ์ค˜์•ผํ•˜๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  ๊ผฌ์ด๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ์‚ฝ์ž…, ์‚ญ์ œ ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•˜์ž๋‹ˆ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจํ•  ๊ฒƒ์ด ๋ป”ํ•˜๋”๋ผ๊ตฌ์š”๐Ÿฅบ ๊ฒฐ๊ตญ ์ด ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด๋ณด๋‹ค ๋” ํšจ์œจ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ฐพ์•„ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ์š”,

Array๋ณด๋‹ค ๋ฐ์ดํ„ฐ ์‚ฝ์ž…, ์‚ญ์ œ๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ธ LinkedList๋ฅผ ํ™œ์šฉํ•ด์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

LinkedList๋ฅผ ์ด์šฉํ•œ ํ’€์ด ๋ฐฉ๋ฒ•

ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ํ…Œ์ด๋ธ”์˜ ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋‹ค์‹œ ๋ณต๊ตฌํ•˜๋Š” ๋“ฑ์˜ ์—ฐ์‚ฐ์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ค‘์—์„œ๋„ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์— ์•ž์„œ ๋…ธ๋“œ๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ•˜๋ฉด ์ข‹์„์ง€(์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„์ง€), ์–ด๋–ค ์—ฐ์‚ฐ์ด ํ•„์š”ํ•œ์ง€๋ฅผ ๋จผ์ € ์ฒดํฌํ•ด์•ผ๊ฒ ์ฃ ?

 

1. ๋…ธ๋“œ ๊ตฌ์„ฑ

์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ ๋…ธ๋“œ์™€ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ(prev, next)๊ฐ€ ํ•˜๋‚˜์”ฉ ํ•„์š”ํ•  ๊ฒƒ์ด๊ณ , ๋ฐ์ดํ„ฐ ํ•„๋“œ์—๋Š” ํ–‰ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Int ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

class Node {
    var index: Int?
    var next: Node?
    var prev: Node?
    
    init(index: Int, next: Node?, prev: Node?) {
        self.index = index
        self.next = next
        self.prev = prev
    }
}

 

2. ์—ฐ์‚ฐ์˜ ์ข…๋ฅ˜

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์‚ฝ์ž… (์ดˆ๊ธฐ ํ…Œ์ด๋ธ”์„ LinkedList๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด)
  • ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋…ธ๋“œ ์‚ญ์ œ (๋ช…๋ น์–ด C์— ํ•ด๋‹น)
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ญ์ œํ•œ ๋…ธ๋“œ ๋ณต์› (๋ช…๋ น์–ด Z์— ํ•ด๋‹น)
  • ์„ ํƒ๋œ ํ–‰ ์œ„์•„๋ž˜๋กœ ์ด๋™ (๋ช…๋ น์–ด U, D์— ํ•ด๋‹น)

LinkedList ํด๋ž˜์Šค์— ์œ„์™€ ๊ฐ™์ด ํ•„์š”ํ•œ ์—ฐ์‚ฐ๋“ค์„ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด๋‘๊ณ , solution ํ•จ์ˆ˜ ์•ˆ์—์„œ switch ๋ฌธ์„ ์ด์šฉํ•ด ๊ฐ ๋ช…๋ น์–ด์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์„ ํ•ด์„œ ์ตœ์ข…์ ์ธ ๊ฐ’์„ ๋„์ถœํ•˜๋„๋ก ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„์— ๊ฐ ํ•จ์ˆ˜๋‚˜ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช…๋„ ํ•จ๊ป˜ ์จ๋‘์—ˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ด๊ฐ€ ๊ฐˆ ๊ฒƒ ๊ฐ™์•„์š”!(์ดํ•ดํ•˜๋Š” ๋ฐ ํฌ๊ฒŒ ์–ด๋ ค์›€์€ ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

 

+ ํ•ด๋‹น ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ํ‘œ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ์ด๋™์€ ์ฃผ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„ ํƒ๋˜๋Š” ํ–‰์„ ์ด๋™ํ•˜๋Š” ์—ฐ์‚ฐ(remove(), moveUp(), moveDown()์— ํฌํ•จ)์—์„œ ํ…Œ์ด๋ธ”์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ์˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์•˜๋Š”๋ฐ์š”, ๋งŒ์•ฝ ์ด๋Ÿฌํ•œ ์กฐ๊ฑด์ด ์—†๋‹ค๋ฉด head์™€ tail์ด nil์ธ์ง€๋ฅผ ์ฒดํฌํ•ด ์ ์ ˆํ•œ ํ–‰์ด ์„ ํƒ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”!

Swift ํ’€์ด ์ฝ”๋“œ

import Foundation

class Node {
    var index: Int?
    var next: Node?
    var prev: Node?
    
    init(index: Int, next: Node?, prev: Node?) {
        self.index = index
        self.next = next
        self.prev = prev
    }
}

class LinkedList {
    var head: Node?
    var tail: Node?
    var cursor: Node?
    var isDeleted: [Bool] = []
    
    // ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์‚ฝ์ž…
    func append(index: Int, isInitCursor: Bool) {
        let node = Node(index: index, next: nil, prev: nil)
        if isInitCursor { cursor = node } // ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋…ธ๋“œ ์„ค์ •
        
        if head == nil {
            head = node
            tail = node
        } else { // ๋’ค์— ์‚ฝ์ž…(์‚ฝ์ž…๋˜๋Š” ๋…ธ๋“œ๊ฐ€ ์ƒˆ๋กœ์šด tail์ด ๋จ)
            node.prev = tail
            tail?.next = node
            tail = node
        }
        isDeleted.append(false)
    }
    
    // ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋…ธ๋“œ ์‚ญ์ œ
    func remove() -> Node? {
        let deleteNode = cursor
        
        cursor?.next?.prev = cursor?.prev
        cursor?.prev?.next = cursor?.next
        // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ด๋ฉด ์ปค์„œ๋ฅผ ์•ž๋…ธ๋“œ๋กœ ์„ค์ •
        cursor = deleteNode?.next == nil ? deleteNode?.prev : deleteNode?.next
        
        isDeleted[deleteNode!.index!] = true
        return deleteNode
    }
    
    // ์ด์ „ ์‚ญ์ œ ๋…ธ๋“œ ๋ณต์›
    func restore(node: Node?) {
        node?.prev?.next = node
        node?.next?.prev = node
        isDeleted[node!.index!] = false
    }
    
    // ์ปค์„œ num๋งŒํผ ์œ„๋กœ ์ด๋™
    func moveUp(to num: Int) {
        for _ in 0..<num {
            cursor = cursor?.prev
        }
    }
    
    // ์ปค์„œ num๋งŒํผ ์•„๋ž˜๋กœ ์ด๋™
    func moveDown(to num: Int) {
        for _ in 0..<num {
            cursor = cursor?.next
        }
    }
}

func solution(_ n:Int, _ k:Int, _ cmd:[String]) -> String {
    var table = LinkedList()
    var deleteStack: [Node?] = []
    
    for i in 0..<n {
        table.append(index: i, isInitCursor: i == k)
    }
    
    for command in cmd {
        let splitCommand = command.split(separator: " ")
        switch splitCommand[0] {
        case "D":
            table.moveDown(to: Int(splitCommand[1])!)
        case "U":
            table.moveUp(to: Int(splitCommand[1])!)
        case "C":
            deleteStack.append(table.remove())
        case "Z":
            table.restore(node: deleteStack.removeLast())
        default:
            break
        }
    }
    
    return table.isDeleted.map({ $0 ? "X" : "O" }).joined(separator: "")
}

 


[์ฐธ๊ณ  ์ž๋ฃŒ]

 

Swift) ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Doubly LinkedList) ๊ตฌํ˜„ ํ•ด๋ณด๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š” :) ์†Œ๋“ค์ž…๋‹ˆ๋‹ค ์ด์ œ ์—ฐํœด๊ฐ€ ์ •๋ง ์—†๊ตฐ์š”....! ๋นก๊ณต ๋ชจ๋“œ On~~ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„  ์ €๋ฒˆ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์™„ํ•œ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ณด๋ ค๊ณ  ํ•ด์š” :) ๋‹จ๋ฐฉํ–ฅ๋ณด๋‹ค๋Š” ์กฐ

babbab2.tistory.com

 

[์Šค์œ„ํ”„ํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค: ํ‘œ ํŽธ์ง‘

๋ฌธ์ œ https://programmers.co.kr/learn/courses/30/lessons/81303 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ‘œ ํŽธ์ง‘ 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" programmers.co.kr [๋ณธ

jeonyeohun.tistory.com