Skip to content

Commit dd6ec45

Browse files
authored
Merge pull request neetcode-gh#3747 from drxlx/2013-detect-squares
Create 2013-detect-squares.swift
2 parents df08b81 + 5640128 commit dd6ec45

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

swift/2013-detect-squares.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class DetectSquares {
3+
4+
var pts = [[Int]]()
5+
var ptsCount = [[Int]: Int]()
6+
7+
init() {
8+
9+
}
10+
11+
func add(_ point: [Int]) {
12+
ptsCount[point, default: 0] += 1
13+
pts.append(point)
14+
}
15+
16+
func count(_ point: [Int]) -> Int {
17+
var res = 0
18+
var x = point[0], y = point[1]
19+
for val in pts {
20+
let px = val[0], py = val[1]
21+
if abs(py - y) != abs(px - x) || x == px || y == py {
22+
continue
23+
}
24+
res += ptsCount[[x, py], default: 0] * ptsCount[[px, y], default: 0]
25+
}
26+
return res
27+
}
28+
}
29+
30+
/**
31+
* Your DetectSquares object will be instantiated and called as such:
32+
* let obj = DetectSquares()
33+
* obj.add(point)
34+
* let ret_2: Int = obj.count(point)
35+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy