Skip to content

Commit

Permalink
0737-sentence-similarity-ii
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 21, 2018
1 parent 1c2ea29 commit 47c037a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
35 changes: 15 additions & 20 deletions 0737-sentence-similarity-ii.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,28 @@ struct UnionFind<T: Hashable> {
}

mutating func setOf(_ element: T) -> Int? {
if let indexOfElement = index[element] {
return setByIndex(indexOfElement)
} else {
return nil
}
guard let indexOfElement = index[element] else { return nil }
return setByIndex(indexOfElement)
}

mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
if firstSet != secondSet {
if size[firstSet] < size[secondSet] {
parent[firstSet] = secondSet
size[secondSet] += size[firstSet]
} else {
parent[secondSet] = firstSet
size[firstSet] += size[secondSet]
}
}
guard let firstSet = setOf(firstElement),
let secondSet = setOf(secondElement),
firstSet != secondSet
else { return }
if size[firstSet] < size[secondSet] {
parent[firstSet] = secondSet
size[secondSet] += size[firstSet]
} else {
parent[secondSet] = firstSet
size[firstSet] += size[secondSet]
}
}

mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
return firstSet == secondSet
} else {
return false
}
guard let firstSet = setOf(firstElement), let secondSet = setOf(secondElement)
else { return false }
return firstSet == secondSet
}
}

Expand Down
72 changes: 72 additions & 0 deletions 0803-bricks-falling-when-hit.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// 803. Bricks Falling When Hit
/// We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and
/// only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent
/// bricks will not drop.
/// We will do some erasures sequentially. Each time we want to do the erasure at the
/// location (i, j), the brick (if it exists) on that location will disappear, and then some other
/// bricks may drop because of that erasure.
/// Return an array representing the number of bricks that will drop after each erasure in sequence.

import XCTest

struct UnionFind<T: Hashable> {
private var index: [T: Int] = [:]
private var parent: [Int] = []
private var size: [Int] = []

mutating func addSetWith(_ element: T) {
index[element] = parent.count
parent.append(parent.count)
size.append(1)
}

/// Path Compression.
private mutating func setByIndex(_ index: Int) -> Int {
if index != parent[index] {
parent[index] = setByIndex(parent[index])
}
return parent[index]
}

mutating func setOf(_ element: T) -> Int? {
guard let indexOfElement = index[element] else { return nil }
return setByIndex(indexOfElement)
}

mutating func sizeOf(_ element: T) -> Int {
guard let set = setOf(element) else { return 0 }
return size[set]
}

mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
guard let firstSet = setOf(firstElement),
let secondSet = setOf(secondElement),
firstSet != secondSet
else { return }
if size[firstSet] < size[secondSet] {
parent[firstSet] = secondSet
size[secondSet] += size[firstSet]
} else {
parent[secondSet] = firstSet
size[firstSet] += size[secondSet]
}
}

mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
guard let firstSet = setOf(firstElement), let secondSet = setOf(secondElement)
else { return false }
return firstSet == secondSet
}
}

/// Approach: Union Find
func hitBricks(_ grid: [[Int]], _ hits: [[Int]]) -> [Int] {

}

class Tests: XCTestCase {
func testExample() {
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0803-bricks-falling-when-hit.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 47c037a

Please sign in to comment.