Skip to content

Commit

Permalink
unsolve/#173: 프로그래머스 파괴되지 않은 건물
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-muuu committed Oct 26, 2023
1 parent e916dd6 commit 3eeab58
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion AlgorithmReview/Programmers/Lv3/파괴되지 않은 건물.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

func solution(_ board:[[Int]], _ skill:[[Int]]) -> Int {
func timeout(_ board:[[Int]], _ skill:[[Int]]) -> Int {

let (row, col) = (board.count, board[0].count)

Expand All @@ -31,3 +31,44 @@ func solution(_ board:[[Int]], _ skill:[[Int]]) -> Int {
}
return ans
}

func reference(_ board:[[Int]], _ skill:[[Int]]) -> Int {

let (row, col) = (board.count, board[0].count)
var temp = [[Int]](repeating: [Int](repeating: 0, count: col+1), count: row+1)

for s in skill {
let (type, r1, c1, r2, c2) = (s[0], s[1], s[2], s[3], s[4])
let degree = type == 2 ? s[5] : -s[5]
temp[r1][c1] += degree
temp[r1][c2+1] += -degree
temp[r2+1][c1] += -degree
temp[r2+1][c2+1] += degree
}

// 행 기준 누적합
for i in 0..<row{
for j in 0..<col{
temp[i][j+1] += temp[i][j]
}
}

// 열 기준 누적합
for i in 0..<row{
for j in 0..<col{
temp[i+1][j] += temp[i][j]
}
}

var board = board
var ans = 0
for i in 0..<row{
for j in 0..<col{
board[i][j] += temp[i][j]
if board[i][j] > 0{
ans += 1
}
}
}
return ans
}

0 comments on commit 3eeab58

Please sign in to comment.