Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 10811] ๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 10811] ๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ (Swift)

๋น„๋น„ bibi 2023. 1. 19. 18:17

10811๋ฒˆ: ๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ

n๊ฐœ์˜ ๋ฐ”๊ตฌ๋‹ˆ๋“ค์ด ์žˆ์„ ๋•Œ, m๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ ์ˆœ์„œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋งŒ๋“ ๋‹ค.

m๊ฐœ์˜ ์‹œํ–‰๋งˆ๋‹ค i๋ถ€ํ„ฐ j๊นŒ์ง€๋ผ๋Š” ๋ฒ”์œ„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค i โ‰ค j โ‰ค n)

  • i == j ์ผ ๋•Œ๋Š” ๋ฐ”๊ฟ€ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • j - i == 1 ์ผ ๋•Œ๋Š” ๋‘ ์š”์†Œ์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค
    • ``swapAt(*:*:)` ์‚ฌ์šฉ
  • ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ์—๋Š” i๋ถ€ํ„ฐ j๊นŒ์ง€์˜ ๋ฐ”๊ตฌ๋‹ˆ๋“ค์„ ๋’ค์ง‘์–ด์•ผ ํ•œ๋‹ค.
    • i ๋ถ€ํ„ฐ j๊นŒ์ง€ ๋’ค์ง‘๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋“ค ์™ธ์— ๋‚˜๋จธ์ง€ ๋ฐ”๊ตฌ๋‹ˆ๋“ค์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค.
    • ๋’ค์ง‘๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋“ค์˜ ์•ž ๋ถ€๋ถ„ = head : ๊ทธ๋Œ€๋กœ ์œ ์ง€
    • ๋’ค์ง‘๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋“ค = reversed
      • reversed() ์‚ฌ์šฉ
    • ๋’ค์ง‘๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋“ค์˜ ๋’ท ๋ถ€๋ถ„ = tail : ๊ทธ๋Œ€๋กœ ์œ ์ง€
    • head + reversed + tail ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด๋“ค์„ ๋ชจ๋‘ ํ•ฉ์ณ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“  ๋‹ค์Œ, ๊ทธ ๋ฐฐ์—ด์„ ์ƒˆ๋กœ์šด ๋ฐ”๊ตฌ๋‹ˆ๋“ค๋กœ ํ• ๋‹นํ•œ๋‹ค.

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๋’ค์ง‘๋Š” ์ž‘์—… ์ž์ฒด๊ฐ€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š๋„๋ก, ๊ธฐ๋ณธ์ œ๊ณต ๋ฉ”์„œ๋“œ๋“ค ์ค‘ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‚ฎ์€ ๋ฉ”์„œ๋“œ๋“ค ์œ„์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋ ค ํ–ˆ๋‹ค.

๋ฌธ์ œ ํ’€์ด์— ์‚ฌ์šฉํ•œ ``swapAt(*:*:)์™€reversed()` ๋ชจ๋‘ O(1) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์•˜๋‹ค. (์ •๋ฆฌํ•œ ๋ฌธ์„œ)


ํ’€์ด (๋‚˜์˜ ํ’€์ด)

let inputArr = readLine()!.split(separator: " ").map { Int($0)! }
let n = inputArr[0]
let m = inputArr[1]
var baskets = Array(1...n)
for _ in (1...m) {
    let exchange = readLine()!.split(separator: " ").map { Int($0)! }
    let start = exchange[0] - 1
    let end = exchange[1] - 1
    if start == end {
        continue
    }
    if end - start == 1 {
        baskets.swapAt(start, end)
        continue
    }
    let head = Array(baskets[baskets.startIndex..<start])
    let tail = Array(baskets[end+1..<baskets.endIndex])
    let reversed = Array( baskets[start...end].reversed())
    baskets = head + reversed + tail
}
baskets.forEach { print($0, terminator: " ")}

๋‹ค๋ฅธ ํ’€์ด

๊ฐ™์€ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š” ํ’€์ด ์ค‘์— ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ‘ผ ์ฝ”๋“œ๋„ ์žˆ์—ˆ๋‹ค.

์ด ๊ฒฝ์šฐ i๋ถ€ํ„ฐ j๋ผ๋Š” ๋ฒ”์œ„์—์„œ โ€œi์™€ j ์ž๋ฆฌ ๋ฐ”๊พธ๊ธฐ โ†’ i+1๊ณผ j-1 ์ž๋ฆฌ ๋ฐ”๊พธ๊ธฐ โ†’ i+2์™€ j-2 ์ž๋ฆฌ ๋ฐ”๊พธ๊ธฐ โ€ฆโ€ ์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๋’ค์ง‘๊ณ , i+n์ด j+n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์งˆ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

์ง์ ‘ ๋’ค์ง‘์ง€ ์•Š๊ณ  ํ•œ ์Œ์”ฉ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”๋„ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋’ค์ง‘์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

let input = readLine()!.split(separator: " ").map { Int($0)! }
let n = input[0], m = input[1]

var basket = [Int](0...n)

for _ in 0..<m {
    let input = readLine()!.split(separator: " ").map { Int($0)! }
    var start = input[0], end = input[1]
    while start < end {
        basket.swapAt(start, end)
        start += 1
        end -= 1
    }
}

basket[1...].forEach { print($0, terminator: " ") }

๋˜ ๋‹ค๋ฅธ ํ’€์ด์—์„œ, ๋งˆ์ง€๋ง‰ ์ค„ ์ถœ๋ ฅ ๋ถ€๋ถ„์— ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

๋‚ด ๋ฐฉ์‹์€ ๋์— ๊ณต๋ฐฑ์ด ์ƒ๊ธฐ๋Š”๋ฐ, ์•„๋ž˜ ๋ฐฉ์‹์€ ๊ณต๋ฐฑ์ด ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์—์„œ ์ข‹๋‹ค. ๋‚˜๋„ joined๋ฅผ ์ž˜ ํ™œ์šฉํ•ด ๋ด์•ผ๊ฒ ๋‹ค.

// ๋‚ด ๋ฐฉ์‹
baskets.forEach { print($0, terminator: " ")}

// ์ƒˆ๋กœ์šด ๋ฐฉ์‹
print(baskets.map{ String($0) }.joined(separator: " "))