728x90
๋ฐ์ํ
Problem
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
๋ฐ์ํ
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ํ๋ก๊ทธ๋๋จธ์ค ํฐ์ผ๋ชฌ (0) | 2022.02.06 |
---|---|
[Swift] 2022 KAKAO BLIND RECRUITMENT ์ฌ๋ผ์ง๋ ๋ฐํ (0) | 2022.01.20 |
[Swift] 2022 KAKAO BLIND RECRUITMENT ์๊ณผ ๋๋ (0) | 2022.01.19 |
[Swift] 2022 KAKAO BLIND RECRUITMENT ์๊ถ๋ํ (1) | 2022.01.19 |
[Swift] 2022 KAKAO BLIND RECRUITMENT ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (0) | 2022.01.19 |
๋๊ธ