10816 숫자 카드2, 10828 스택, 10845 큐, 10866 덱

Choong Won, Seo·2022년 1월 7일
0

백준

목록 보기
12/28
post-thumbnail

Today 1/7

숫자 카드 2 (My Code)

let _ = Int(readLine()!)!
let inputArr = readLine()!.split(separator: " ").map{Int(String($0))!}
var dict = [Int : Int]()
for input in inputArr {
    if let value = dict[input] {
        dict[input] = value + 1
    } else {
        dict[input] = 1
    }
}
let _ = Int(readLine()!)!
let numArr = readLine()!.split(separator: " ").map{Int(String($0))!}
var outputArr = [String]()
for num in numArr {
    if let count = dict[num] {
        outputArr.append(String(count))
    } else {
        outputArr.append("0")
    }
}

print(outputArr.joined(separator: " "))

배열에 저장하고 몇 개 있는지 하나씩 세면 분명히 시간초과가 날 것 같아서 Dictionary 타입으로 저장을 하고, 같은 key가 나올 때마다 value를 하나씩 +하는 방법으로 풀었다.

스택 (My Code)

var stack = [Int]()

for _ in 0..<Int(readLine()!)! {
    let input = readLine()!.split(separator: " ").map{String($0)}
    command(input)
}

func command(_ input: [String]) {
    switch input[0] {
    case "push":
        stack.append(Int(input[1])!)
    case "pop":
        if let last = stack.last {
            stack.removeLast()
            print(last)
        } else {
            print(-1)
        }
    case "size":
        print(stack.count)
    case "empty":
        print(stack.isEmpty ? 1 : 0)
    case "top":
        print((stack.last != nil) ? stack.last! : -1)
    default:
        return
    }
}

조금 더 개선해보기로 했다. stack.last가 있는지 검사하는 것 보다 stack.isEmpty로 빈 스택인지를 확인하는 방법이 더 빨라서 그 방법으로 개선했다.

var stack = [Int]()

for _ in 0..<Int(readLine()!)! {
    let input = readLine()!.split(separator: " ").map{String($0)}
    command(input)
}

func command(_ input: [String]) {
    switch input[0] {
    case "push":
        stack.append(Int(input[1])!)
    case "pop":
        if stack.isEmpty {
            print(-1)
        } else {
            print(stack.removeLast())
        }
    case "size":
        print(stack.count)
    case "empty":
        print(stack.isEmpty ? 1 : 0)
    case "top":
        print(stack.isEmpty ? -1 : stack.last!)
    default:
        return
    }
}

큐 (My Code)

var queue = [Int]()

for _ in 0..<Int(readLine()!)! {
    let input = readLine()!.split(separator: " ").map{String($0)}
    command(input)
}

func command(_ input: [String]) {
    switch input[0] {
    case "push":
        queue.append(Int(input[1])!)
    case "pop":
        if queue.isEmpty {
            print(-1)
        } else {
            print(queue.removeFirst())
        }
    case "size":
        print(queue.count)
    case "empty":
        print(queue.isEmpty ? 1 : 0)
    case "front":
        print(queue.isEmpty ? -1 : queue.first!)
    case "back":
        print(queue.isEmpty ? -1 : queue.last!)
    default:
        return
    }
}

큐 또한 스택과 동일하다.

덱 (My Code)

var deck = [Int]()

for _ in 0..<Int(readLine()!)! {
    let input = readLine()!.split(separator: " ").map{String($0)}
    command(input)
}

func command(_ input: [String]) {
    switch input[0] {
    case "push_front":
        deck.insert(Int(input[1])!, at: 0)
    case "push_back":
        deck.append(Int(input[1])!)
    case "pop_front":
        if deck.isEmpty {
            print(-1)
        } else {
            print(deck.removeFirst())
        }
    case "pop_back":
        if deck.isEmpty {
            print(-1)
        } else {
            print(deck.removeLast())
        }
    case "size":
        print(deck.count)
    case "empty":
        print(deck.isEmpty ? 1 : 0)
    case "front":
        print(deck.isEmpty ? -1 : deck.first!)
    case "back":
        print(deck.isEmpty ? -1 : deck.last!)
    default:
        return
    }
}

동일하다.

profile
UXUI Design Based IOS Developer

0개의 댓글