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

[๋ฐฑ์ค€ 4493] ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด? (Swift) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 4493] ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด? (Swift)

๋น„๋น„ bibi 2022. 10. 20. 23:31

4493๋ฒˆ: ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด?

  • ๋งจ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐฏ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค
  • ๊ทธ ์•„๋ž˜๋ถ€ํ„ฐ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์ฃผ์–ด์ง„๋‹ค
    • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ ์ค„์€ ๊ฐ€์œ„๋ฐ”์œ„๋ณด ๊ฒŒ์ž„ ํšŸ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค
    • ๊ทธ ์•„๋ž˜๋ถ€ํ„ฐ๋Š” ๊ฐ€์œ„๋ฐ”์œ„๋ณด ๊ฒŒ์ž„ ํšŸ์ˆ˜๋งŒํผ ๋ฌด์—‡์„ ๋ƒˆ๋Š”์ง€ ์ฃผ์–ด์ง„๋‹ค
      • ์™ผ์ชฝ์€ Player1, ์˜ค๋ฅธ์ชฝ์€ Player2
      • R = Rock = ๋ฐ”์œ„, S = Scissor = ๊ฐ€์œ„, P = Paper = ๋ณด
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋ˆ„๊ฐ€ ์ด๊ฒผ๋Š”์ง€(๋˜๋Š” ๋น„๊ฒผ๋Š”์ง€) ์ถœ๋ ฅํ•œ๋‹ค

ํ’€์ด

let count = Int(readLine()!)!
(1...count).forEach { num in
    let game = Int(readLine()!)!
    var score1 = 0
    var score2 = 0
    (1...game).forEach { _ in
        let inputArr = readLine()!.split(separator: " ")
        if inputArr.contains("R") && inputArr.contains("S") { // O(n)..
            if inputArr.first! == "R" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
        if inputArr.contains("S") && inputArr.contains("P") {
            if inputArr.first! == "S" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
        if inputArr.contains("P") && inputArr.contains("R") {
            if inputArr.first! == "P" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
    }
    print(score1, score2)
    if score1 > score2 {
        print("Player 1")
    } else if score1 < score2 {
        print("Player 2")
    } else {
        print("TIE")
    }
}

Player1์ด ๋‚ธ ํŒจ์— ๋Œ€ํ•ด Player2๊ฐ€ ๋‚ธ ํŒจ๊ฐ€ ์ด๊ธฐ๋Š”์ง€/๋น„๊ธฐ๋Š”์ง€/์ง€๋Š”์ง€ ๋ฅผ ๋น„๊ตํ•  ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ,

๋‚˜๋Š” ์ด๊ธฐ๊ณ  ์ง€๋Š” ํŒจ์˜ ์กฐํ•ฉ (๊ฐ€์œ„, ๋ฐ”์œ„ / ๋ฐ”์œ„, ๋ณด / ๊ฐ€์œ„, ๋ณด) ์— ๋Œ€ํ•ด์„œ๋งŒ ๋น„๊ต๋ฅผ ํ•˜๋Š” ํ’€์ด๊ฐ€ ๋จผ์ € ๋– ์˜ฌ๋ผ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ ๋น„๊ธธ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ์Šค์ฝ”์–ด๋ฅผ ์ •ํ•˜๋Š๋ƒ?? ์‚ฌ์‹ค ์ƒ๊ฐ์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์ด์—ˆ๊ณ  ์ง€๊ธˆ ํ’€์ด๋ฅผ ์ ์œผ๋ฉด์„œ ๋†“์ณค์Œ์„ ๊นจ๋‹ฌ์•˜์ง€๋งŒ..๐Ÿฅฒ ์–ด์ฐจํ”ผ ๋น„๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” ๋‘˜ ์ค‘ ์•„๋ฌด ์ชฝ์—๋„ ์ ์ˆ˜๋ฅผ ๋”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์ŠนํŒจ์˜ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค.

(์ฑ„์ ์„ ํ•ด ๋ณด๋‹ˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™์ง„ ์•Š์•„์„œ ์•„์‰ฝ๋‹ค)

์‹œ๊ฐ„๋ณต์žก๋„๋Š” 60ms๊ฐ€ ๋‚˜์™”๋‹ค.

๋ณด์™„์ 

if inputArr.contains("R") && inputArr.contains("S") { ... }

์ด ๋ถ€๋ถ„์€ contains ํ•จ์ˆ˜๊ฐ€ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ,

(์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ด๋‹ˆ ์ž…๋ ฅ์ด ์ผ์ •ํ•˜๋‹ค๋Š” ๊ฐ€์ • ํ•˜์—) ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ์ ‘๊ทผ์œผ๋กœ ํ’€์—ˆ๋‹ค๋ฉด ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™๋‹ค.

let count = Int(readLine()!)!
(1...count).forEach { num in
    let game = Int(readLine()!)!
    var score1 = 0
    var score2 = 0
    (1...game).forEach { _ in
        let inputArr = readLine()!.split(separator: " ")
        if inputArr[0] == "R" && inputArr[1] == "S"
                        || inputArr[0] == "S" && inputArr[1] == "R" {
            if inputArr.first! == "R" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
        if inputArr[0] == "S" && inputArr[1] == "P"
                        || inputArr[0] == "P" && inputArr[1] == "S" {
            if inputArr.first! == "S" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
        if inputArr[0] == "P" && inputArr[1] == "R"
                        || inputArr[0] == "R" && inputArr[1] == "P" {
            if inputArr.first! == "P" {
                score1 += 1
            } else {
                score2 += 1
            }
        }
    }
    if score1 > score2 {
        print("Player 1")
    } else if score1 < score2 {
        print("Player 2")
    } else {
        print("TIE")
    }
}

์กฐ๊ฑด์ ˆ ๋ถ€๋ถ„์ด ๋‘๋ฐฐ๋กœ ๊ธธ์–ด์ง€๊ธด ํ–ˆ์ง€๋งŒ ์ˆ˜์ •ํ•ด ๋ณด์•˜๋‹ค (๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค)

์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ด์ชฝ์ด 56ms๋กœ ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์™”๋‹ค! ๐Ÿ™‚