๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋‚˜๋ฆ„๋Œ€๋กœ ํ’€์—ˆ๋Š”๋ฐ, ํ‹€๋ ธ๊ฑฐ๋‚˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ์ธ ์ฝ”๋“œ ๋ชจ์Œ์ง‘

 

BOJ: #2870 - ์ˆ˜ํ•™์ˆ™์ œ

์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋Š” ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋Š” ๋ฐฐ์—ด์— ๋”ฐ๋กœ ์ €์žฅํ•ด์„œ ์ฒ˜๋ฆฌํ–ˆ๋‹ค. ๋ชจ๋“  ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ์—๋Š” allSatisfy()๋ฅผ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ–ˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  0์„ ์ •๊ทœ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๋ ฌ์€ swift string number sort๋กœ ๊ตฌ๊ธ€๋งํ•ด์„œ ๋‚˜์˜จ ๊ฑธ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ทผ๋ฐ ํ‹€๋ ธ์Œ… ์™œ ํ‹€๋ ธ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹น

 

๋ฌธ์ œ

https://www.acmicpc.net/problem/2870

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

import Foundation
let n = Int(readLine()!)!
var nums = [String]()
for _ in 0..<n {
    let input = readLine()!
    var num = [Character]()
    for char in input {
        if char.isNumber {
            num.append(char)
        } else {
            if !num.isEmpty {
                if num.allSatisfy({$0 == "0"}) {
                    nums.append("0")
                } else {
                    let trimmed = String(num).replacingOccurrences(of: "^0+", with: "", options: .regularExpression)
                    nums.append(trimmed)
                    num = []
                }
            }
        }
    }

    if !num.isEmpty {
        if num.allSatisfy({$0 == "0"}) {
            nums.append("0")
        } else {
            let trimmed = String(num).replacingOccurrences(of: "^0+", with: "", options: .regularExpression)
            nums.append(trimmed)
            num = []
        }
    }
}

// ์ •๋ ฌ ๋ถ€๋ถ„ ๊ณ ์น˜๊ธฐ
nums.sort(by: { $0.localizedStandardCompare($1) == .orderedAscending})
//nums.sort(by: { $0.compare($1, options: .numeric) == .orderedAscending})
for num in nums {
    print(num)
}

 

BOJ: #2852 - NBA ๋†๊ตฌ

๋Œ€ํšŒ์—์„œ ์ถœ์ œ๋๋˜ ๋ฌธ์ œ๋ผ ๋Œ€ํšŒ ํ™ˆํŽ˜์ด์ง€์—์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ดค๋‹ค. ์ค‘๊ฐ„์— ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ์ž˜๋ชปํ–ˆ๋‹ค๋Š” ๊ฑธ ๋Š๊ผˆ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๊ณ ์ณ์•ผํ•  ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋ณด๋ฅ˜.

 

๋ฌธ์ œ

https://www.acmicpc.net/problem/2852

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

import Foundation
let n = Int(readLine()!)!
var info = [[String]]()

for _ in 0..<n {
    let victory = readLine()!.split(separator: " ").map { String($0) }
    info.append(victory)
}

var prevWin = info[0][0]
var prevTime = info[0][1].split(separator: ":").map { Int(String($0))! }
var firstTeam = [0, 0]
var secondTeam = [0, 0]

for i in 1..<n {
    if prevWin != info[i][0] { // ์ด๊ธฐ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„
        let time = info[i][1].split(separator: ":").map { Int(String($0))! }
        var minute = time[0] - prevTime[0]
        var second = time[1] - prevTime[1]
        if second < 0 {
            minute -= 1
            second += 60
        }

        if prevWin == "1" { // 1๋ฒˆ ํŒ€์ด ์ด๊ธฐ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„
            firstTeam[0] += minute
            firstTeam[1] += second
        } else { // 2๋ฒˆ ํŒ€์ด ์ด๊ธฐ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„
            secondTeam[0] += minute
            secondTeam[1] += second
        }

        prevWin = info[i][0]
        prevTime = time
    }
}

if prevWin == info[n-1][0] { // ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ด๊ธฐ๊ณ  ์žˆ์—ˆ์œผ๋ฉด
    let time = info[n-1][1].split(separator: ":").map { Int(String($0))! }
    var overMinute = 48 - time[0]
    var overSecond = -time[1]
    if overSecond < 0 {
        overMinute -= 1
        overSecond += 60
    }

    if prevWin == "1" { // 1๋ฒˆ ํŒ€์ด ์ด๊ธฐ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„
        firstTeam[0] += overMinute
        firstTeam[1] += overSecond
    } else { // 2๋ฒˆ ํŒ€์ด ์ด๊ธฐ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„
        secondTeam[0] += overMinute
        secondTeam[1] += overSecond
    }
}

print(String(format: "%02d", firstTeam[0]) + ":" + String(format: "%02d", firstTeam[1]))
print(String(format: "%02d", secondTeam[0]) + ":" + String(format: "%02d", secondTeam[1]))

 

BOJ: #1436 - ์˜ํ™”๊ฐ๋… ์ˆŒ

๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์„œ 666๋ถ€ํ„ฐ n๊ฐœ์˜ 666์ด ๋“ค์–ด๊ฐ€๋Š” ์ˆซ์ž ๋ฐฐ์—ด๊นŒ์ง€ ์ˆœํšŒํ•ด์„œ ์‚ฝ์ž…ํ–ˆ๋Š”๋ฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ. ํ•˜๊ธด, Xcode์—์„œ 9999๋ฅผ ํ•  ๋•Œ ๋Š๋ฆฌ๊ธด ํ–ˆ๋‹ค. ์‹œ๊ฐ„์„ ๋” ์–ด๋–ป๊ฒŒ ์ค„์—ฌ์•ผํ•  ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ํŒจ์Šค.

 

๋ฌธ์ œ

https://www.acmicpc.net/problem/1436

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

import Foundation
let n = Int(readLine()!)!
var nums = [String]()
var i = 666
while nums.count != n {
    if String(i).contains("666") {
        nums.append(String(i))
    }
    i += 1
}

print(nums[n-1])

 

BOJ: #14620 - ๊ฝƒ๊ธธ

ํƒ์ƒ‰ ๋ฌธ์ œ์ฒ˜๋Ÿผ ํ’€์—ˆ๋Š”๋ฐ, ์–ด๋””๊ฐ€ ์ž˜๋ชป๋œ ๊ฑฐ์•ผ…? ๋‚œ ๋ชจ๋ฅด๊ฒ ๋„ค /_\

 

๋ฌธ์ œ

https://www.acmicpc.net/problem/14620

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let n = Int(readLine()!)!
var price = [[Int]]()
for _ in 0..<n {
    let line = readLine()!.split(separator: " ").map { Int(String($0))! }
    price.append(line)
}

var seeds = 3
var board = Array(repeating: Array(repeating: 0, count: n), count: n)
var ans = 0
while seeds != 0 {
    var dict = [[Int]:Int]()
    for i in 0..<n {
        for j in 0..<n {
            // ๊ฝƒ์ด ์˜จ์ „ํžˆ ํ”ผ์–ด์•ผํ•จ
            if j - 1 < 0 || j + 1 >= n || i - 1 < 0 || i + 1 >= n {
                continue
            }

            // ์ด๋ฏธ ๊ฝƒ์„ ์‹ฌ์€ ๊ณณ์€ ์•ˆ๋จ
            if board[i][j] == 1 || board[i][j-1] == 1 || board[i][j+1] == 1 || board[i+1][j] == 1 || board[i-1][j] == 1 {
                continue
            }

            // ์ผ๋‹จ ๊ฐ€๊ฒฉ๊ณผ ์œ„์น˜๋ฅผ ์ €์žฅํ•ด๋‘์ž
            let sum = price[i][j] + price[i][j+1] + price[i][j-1] + price[i+1][j] + price[i-1][j]
            dict[[i, j]] = sum
        }
    }


    if let minPos = dict.min(by: { $0.value <= $1.value }) {
        ans += minPos.value
        board[minPos.key[0]][minPos.key[1]] = 1
        board[minPos.key[0]][minPos.key[1] + 1] = 1
        board[minPos.key[0]][minPos.key[1] - 1] = 1
        board[minPos.key[0] + 1][minPos.key[1]] = 1
        board[minPos.key[0] - 1][minPos.key[1]] = 1
        seeds -= 1
    }
}

print(ans)

 

BOJ: #4195 - ์นœ๊ตฌ ๋„คํŠธ์›Œํฌ

BFS/DFS ์œ ํ˜•์˜ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ, 31% ์ฏค์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ. ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ๋ณด๋‹ˆ๊นŒ ์ด๋Ÿฐ์‹์œผ๋กœ ํ‘ธ๋Š” ๊ฑฐ ๊ฐ™์ง„ ์•Š์€๋ฐ ๋‹ค๋ฅธ ๋ฐฉ์‹์€ ์ƒ๊ฐ์ด ์•ˆ ๋‚˜์„œ ํŒจ์Šค.

 

๋ฌธ์ œ

https://www.acmicpc.net/problem/4195

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let t = Int(readLine()!)!
for _ in 0..<t {
    let f = Int(readLine()!)!
    var graph = [String: [String]]()
    for _ in 0..<f {
        let friend = readLine()!.split(separator: " ").map { String($0) }
        // ์–‘๋ฐฉํ–ฅ ์นœ๊ตฌ ๊ด€๊ณ„
        graph[friend[0], default: []].append(friend[1])
        graph[friend[1], default: []].append(friend[0])
        // ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ์ƒ๊ธธ ๋•Œ๋งˆ๋‹ค ๋„คํŠธ์›Œํฌ ํƒ์ƒ‰ํ•˜๊ธฐ
        var visited: Set = [friend[0]]
        var queue: Set = [friend[0]]
        while !queue.isEmpty {
            let v = queue.removeFirst()

            for w in graph[v]! {
                if !visited.contains(w) {
                    visited.insert(w)
                    queue.insert(w)
                }
            }
        }
        print(visited.count)
    }
}
๋ฐ˜์‘ํ˜•