detect_squares

 1from collections import defaultdict
 2
 3
 4# @leet start
 5class DetectSquares:
 6    """
 7    This class detects the squares that are axis aligned given a query point
 8    and 3 points that have been seen in the stream.
 9
10    When adding a point, we just add it to a hashmap.
11
12    When counting, we iterate through all the points in the map,
13    and calculate the axis adjacent points by comparing each with the current point.
14
15    We then calculate the diagonal of each point and then see how many times the points
16    appear in the set to return the final count.
17    """
18
19    def __init__(self):
20        self.points = defaultdict(int)
21
22    def add(self, point: list[int]) -> None:
23        self.points[tuple(point)] += 1
24
25    def count(self, point: list[int]) -> int:
26        count = 0
27        x1, y1 = point
28        for (x2, y2), n in self.points.items():
29            x_dist, y_dist = abs(x1 - x2), abs(y1 - y2)
30            if x_dist == y_dist and x_dist > 0:
31                corner1 = (x1, y2)
32                corner2 = (x2, y1)
33
34                if corner1 in self.points and corner2 in self.points:
35                    count += n * self.points[corner1] * self.points[corner2]
36        return count
37
38
39def test():
40    assert 2 + 2 == 4
class DetectSquares:
 6class DetectSquares:
 7    """
 8    This class detects the squares that are axis aligned given a query point
 9    and 3 points that have been seen in the stream.
10
11    When adding a point, we just add it to a hashmap.
12
13    When counting, we iterate through all the points in the map,
14    and calculate the axis adjacent points by comparing each with the current point.
15
16    We then calculate the diagonal of each point and then see how many times the points
17    appear in the set to return the final count.
18    """
19
20    def __init__(self):
21        self.points = defaultdict(int)
22
23    def add(self, point: list[int]) -> None:
24        self.points[tuple(point)] += 1
25
26    def count(self, point: list[int]) -> int:
27        count = 0
28        x1, y1 = point
29        for (x2, y2), n in self.points.items():
30            x_dist, y_dist = abs(x1 - x2), abs(y1 - y2)
31            if x_dist == y_dist and x_dist > 0:
32                corner1 = (x1, y2)
33                corner2 = (x2, y1)
34
35                if corner1 in self.points and corner2 in self.points:
36                    count += n * self.points[corner1] * self.points[corner2]
37        return count

This class detects the squares that are axis aligned given a query point and 3 points that have been seen in the stream.

When adding a point, we just add it to a hashmap.

When counting, we iterate through all the points in the map, and calculate the axis adjacent points by comparing each with the current point.

We then calculate the diagonal of each point and then see how many times the points appear in the set to return the final count.

points
def add(self, point: list[int]) -> None:
23    def add(self, point: list[int]) -> None:
24        self.points[tuple(point)] += 1
def count(self, point: list[int]) -> int:
26    def count(self, point: list[int]) -> int:
27        count = 0
28        x1, y1 = point
29        for (x2, y2), n in self.points.items():
30            x_dist, y_dist = abs(x1 - x2), abs(y1 - y2)
31            if x_dist == y_dist and x_dist > 0:
32                corner1 = (x1, y2)
33                corner2 = (x2, y1)
34
35                if corner1 in self.points and corner2 in self.points:
36                    count += n * self.points[corner1] * self.points[corner2]
37        return count
def test():
40def test():
41    assert 2 + 2 == 4