๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] 2022 KAKAO BLIND RECRUITMENT ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ

by Fomagran ๐Ÿ’ป 2022. 1. 20.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ

[[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]] [[1,0,0,3,4,4],[1,2,0,2,3,2],[2,1,0,3,1,2],[1,0,1,3,3,1]] 10 [[1,2,3],[4,5,6],[7,8,9]] [[1,1,1,2,2,4],[1,0,0,1,1,2],[2,2,0,2,0,100]] 6

programmers.co.kr


Solution

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ˆ„์ ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

(์ž์„ธํ•œ ์„ค๋ช…์€ ์นด์นด์˜ค ๊ณต์‹ ํ•ด์„ค์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋” ์ดํ•ดํ•˜๊ธฐ ์ˆ˜์›”ํ•˜์‹ค ๊ฑฐ์—์š”!)

 

1. board์—์„œ ์—ด๊ณผ ํ–‰์ด +1๋งŒํผ์˜ ํฌ๊ธฐ์˜ ์ƒˆ๋กœ์šด ๋ณด๋“œ๋ฅผ 0์œผ๋กœ ์ฑ„์›Œ์ค€๋‹ค.

 

 var zeroBoard = Array(repeating: Array(repeating:0, count: board[0].count+1), count:board.count+1)

 

2. ์Šคํ‚ฌ์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ๋ณด๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

 

func changeBoard(_ board:inout [[Int]],_ skill:[Int]){
    let isHeal = skill[0] == 2
    let start = Point(x: skill[2], y: skill[1])
    let end = Point(x: skill[4], y: skill[3])
    let degree = isHeal ? skill[5] : -skill[5]
    board[start.y][start.x] += degree
    board[end.y+1][end.x+1] += degree
    board[start.y][end.x+1] -= degree
    board[end.y+1][start.x] -= degree
}

 

3. ์ˆ˜์ง์œผ๋กœ ๋ˆ„์ ํ•ด์„œ ๋”ํ•œ๋‹ค.

 

func addVertically(_ board:inout [[Int]]) {
    for j in 0..<board[0].count-1 {
        for i in 1..<board.count-1 {
            board[i][j] += board[i-1][j]
        }
    }
}

 

4. ์ˆ˜ํ‰์œผ๋กœ ๋ˆ„์ ํ•ด์„œ ๋”ํ•œ๋‹ค.

 

(์ž๊พธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ์ˆ˜ํ‰์œผ๋กœ ๋”ํ•˜๋ฉด์„œ 0๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.)

 

func addHorizontally(_ board:inout [[Int]],_ original:[[Int]]) -> Int {
    var count = 0
    for i in 0..<board.count-1 {
        for j in 0..<board[0].count-1 {
            if j != 0 {
                board[i][j] += board[i][j-1]
            }
            if board[i][j] + original[i][j] > 0{
                count += 1
            }
        }
    }
    return count
}

 

5. 0๋ณด๋‹ค ํฐ ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

func solution(_ board:[[Int]], _ skill:[[Int]]) -> Int {
    var zeroBoard = Array(repeating: Array(repeating:0, count: board[0].count+1), count:board.count+1)
    for s in skill {
        changeBoard(&zeroBoard, s)
    }
    addVertically(&zeroBoard)
    return addHorizontally(&zeroBoard,board)
}

Source Code

 


P.S

๋ฐฐ์—ด์„ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ด์„œ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๋ฐฉ์‹์„ ์ž์ฃผ ์ด์šฉํ–ˆ๋‹ค.

 

(์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ)

let array:[Int] = []

func updateArray() -> Int {
	var newArray:[Int] = []
    return newArray
}

array = updateArray()

 

ํ•˜์ง€๋งŒ ๊ทธ๋ƒฅ ๋ฐ”๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํšจ์œจ์ ์ด๊ณ  ๋น ๋ฅด๋‹ค... ์ด๊ฑธ ๋ชฐ๋ผ์„œ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ๊ณ ์ƒํ–ˆ๋‹ค...

 

(๋งŒ์•ฝ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด inout์„ ์ด์šฉํ•˜์ž)

 

let array:[Int] = []

func updateArray(_ array:inout [Int]) {
	array[0] = 1
}

updateArray(&array)
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€