From 5640128430938a29ab4603268b5d018817f8f6bb Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh Date: Sat, 23 Nov 2024 11:40:04 +0300 Subject: [PATCH] Create 2013-detect-squares.swift --- swift/2013-detect-squares.swift | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 swift/2013-detect-squares.swift diff --git a/swift/2013-detect-squares.swift b/swift/2013-detect-squares.swift new file mode 100644 index 000000000..09c58c0ff --- /dev/null +++ b/swift/2013-detect-squares.swift @@ -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) + */ \ No newline at end of file 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