Skip to content

Commit 8c7086f

Browse files
authored
Merge pull request #350 from soapyigu/Design
Design
2 parents 55dab32 + 78e53ed commit 8c7086f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

Design/DetectSquares.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/detect-squares/
3+
* Primary idea: Use hashmap to track points having the same x and y and calculate all possible results
4+
*
5+
* Time Complexity: add - O(1), count - O(n)
6+
* Space Complexity: O(n)
7+
*
8+
*/
9+
10+
class DetectSquares {
11+
12+
private var xPoints = [Int: [Int: Int]]()
13+
private var yPoints = [Int: [Int: Int]]()
14+
15+
init() { }
16+
17+
func add(_ point: [Int]) {
18+
let x = point[0], y = point[1]
19+
20+
xPoints[x, default: [y: 0]][y, default: 0] += 1
21+
yPoints[y, default: [x: 0]][x, default: 0] += 1
22+
}
23+
24+
func count(_ point: [Int]) -> Int {
25+
let x = point[0], y = point[1]
26+
27+
guard let xEqualPoints = xPoints[x], let yEqualPoints = yPoints[y] else {
28+
return 0
29+
}
30+
31+
var res = 0
32+
33+
for (yEqualPointX, firstPointsCount) in yEqualPoints {
34+
if x == yEqualPointX {
35+
continue
36+
}
37+
let sideLength = abs(x - yEqualPointX)
38+
// check bottom square
39+
if let secondPointCount = xEqualPoints[y - sideLength] {
40+
if let thirdPointCount = xPoints[yEqualPointX]?[y - sideLength] {
41+
res += firstPointsCount * secondPointCount * thirdPointCount
42+
}
43+
}
44+
if let secondPointCount = xEqualPoints[y + sideLength] {
45+
if let thirdPointCount = xPoints[yEqualPointX]?[y + sideLength] {
46+
res += firstPointsCount * secondPointCount * thirdPointCount
47+
}
48+
}
49+
}
50+
51+
return res
52+
}
53+
}
54+
55+
/**
56+
* Your DetectSquares object will be instantiated and called as such:
57+
* let obj = DetectSquares()
58+
* obj.add(point)
59+
* let ret_2: Int = obj.count(point)
60+
*/

Design/GuessWord.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/guess-the-word/
3+
* Primary idea: Random select a word and check the match count. Filter all words having the same match count.
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
* // This is the Master's API interface.
8+
* // You should not implement it, or speculate about its implementation
9+
* class Master {
10+
* public func guess(word: String) -> Int {}
11+
* }
12+
*/
13+
14+
class GuessWord {
15+
func findSecretWord(_ words: [String], _ master: Master) {
16+
var words = words
17+
18+
for i in 0..<30 {
19+
let trial = words[words.count / 2]
20+
let count = master.guess(trial)
21+
22+
if count == 6 {
23+
return
24+
}
25+
26+
var possibilities = [String]()
27+
for word in words {
28+
if matchCount(trial, word) == count {
29+
possibilities.append(word)
30+
}
31+
}
32+
words = possibilities
33+
}
34+
35+
}
36+
37+
private func matchCount(_ wordA: String, _ wordB: String) -> Int {
38+
var res = 0
39+
40+
for (charA, charB) in zip(wordA, wordB) {
41+
if charA == charB {
42+
res += 1
43+
}
44+
}
45+
46+
return res
47+
}
48+
}

0 commit comments

Comments
 (0)