-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3747 from drxlx/2013-detect-squares
Create 2013-detect-squares.swift
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
class DetectSquares { | ||
|
||
var pts = [[Int]]() | ||
var ptsCount = [[Int]: Int]() | ||
|
||
init() { | ||
|
||
} | ||
|
||
func add(_ point: [Int]) { | ||
ptsCount[point, default: 0] += 1 | ||
pts.append(point) | ||
} | ||
|
||
func count(_ point: [Int]) -> Int { | ||
var res = 0 | ||
var x = point[0], y = point[1] | ||
for val in pts { | ||
let px = val[0], py = val[1] | ||
if abs(py - y) != abs(px - x) || x == px || y == py { | ||
continue | ||
} | ||
res += ptsCount[[x, py], default: 0] * ptsCount[[px, y], default: 0] | ||
} | ||
return res | ||
} | ||
} | ||
|
||
/** | ||
* Your DetectSquares object will be instantiated and called as such: | ||
* let obj = DetectSquares() | ||
* obj.add(point) | ||
* let ret_2: Int = obj.count(point) | ||
*/ |