Skip to content

Commit

Permalink
Merge pull request #3747 from drxlx/2013-detect-squares
Browse files Browse the repository at this point in the history
Create 2013-detect-squares.swift
  • Loading branch information
Ykhan799 authored Nov 24, 2024
2 parents df08b81 + 5640128 commit dd6ec45
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions swift/2013-detect-squares.swift
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)
*/

0 comments on commit dd6ec45

Please sign in to comment.