Skip to content

Commit

Permalink
0803-bricks-falling-when-hit
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 21, 2018
1 parent 47c037a commit d924eb5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
67 changes: 66 additions & 1 deletion 0803-bricks-falling-when-hit.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,76 @@ struct UnionFind<T: Hashable> {

/// Approach: Union Find
func hitBricks(_ grid: [[Int]], _ hits: [[Int]]) -> [Int] {
var dropped = grid
let m = grid.count
guard m > 0 else { return [] }
let n = grid[0].count

// remove all the hits
for hit in hits {
dropped[hit[0]][hit[1]] = 0
}

var dset = UnionFind<Int>()
dset.addSetWith(Int.min) // roof set
for r in 0..<m {
for c in 0..<n {
if dropped[r][c] == 1 {
let index = r * n + c
dset.addSetWith(index)
if r == 0 {
dset.unionSetsContaining(index, and: Int.min)
}
if r > 0 && dropped[r - 1][c] == 1 {
dset.unionSetsContaining(index, and: (r - 1) * n + c)
}
if c > 0 && dropped[r][c - 1] == 1 {
dset.unionSetsContaining(index, and: r * n + c - 1)
}
}
}
}

var result = Array(repeating: 0, count: hits.count)
for i in stride(from: hits.count - 1, through: 0, by: -1) {
let r = hits[i][0]
let c = hits[i][1]
let oldCount = dset.sizeOf(Int.min)
guard grid[r][c] == 1 else { continue }
let index = r * n + c
dset.addSetWith(index)
if r > 0 && dropped[r - 1][c] == 1 {
dset.unionSetsContaining(index, and: (r - 1) * n + c)
}
if r + 1 < m && dropped[r + 1][c] == 1 {
dset.unionSetsContaining(index, and: (r + 1) * n + c)
}
if c > 0 && dropped[r][c - 1] == 1 {
dset.unionSetsContaining(index, and: r * n + c - 1)
}
if c + 1 < n && dropped[r][c + 1] == 1 {
dset.unionSetsContaining(index, and: r * n + c + 1)
}
if r == 0 {
dset.unionSetsContaining(index, and: Int.min)
}
dropped[r][c] = 1
result[i] = max(0, dset.sizeOf(Int.min) - oldCount - 1)
}
return result
}

class Tests: XCTestCase {
func testExample() {
func testExample1() {
let grid = [[1, 0, 0, 0], [1, 1, 1, 0]]
let hits = [[1, 0]]
XCTAssertEqual(hitBricks(grid, hits), [2])
}

func testExample2() {
let grid = [[1], [1], [1], [1], [1]]
let hits = [[3, 0], [4, 0], [1, 0], [2, 0], [0, 0]]
XCTAssertEqual(hitBricks(grid, hits), [1, 0, 1, 0, 0])
}
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ You can visit the pages below to search problems by company tags. Then come back
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window
686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0686-repeated-string-match.playground/Contents.swift) | String
737 | [Sentence Similarity II \*](https://leetcode.com/problems/sentence-similarity-ii/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0737-sentence-similarity-ii.playground/Contents.swift) | Union Find
803 | [Bricks Falling When Hit](https://leetcode.com/problems/bricks-falling-when-hit/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0803-bricks-falling-when-hit.playground/Contents.swift) | Union Find
843 | [Guess the Word \*](https://leetcode.com/problems/guess-the-word/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0843-guess-the-word.playground/Contents.swift) | Minimax

**\* problems are not compilable on LeetCode. I have filed bug report for these problems.**

0 comments on commit d924eb5

Please sign in to comment.