Package g2001_2100.s2013_detect_squares
Class DetectSquares
java.lang.Object
g2001_2100.s2013_detect_squares.DetectSquares
2013 - Detect Squares.<p>Medium</p>
<p>You are given a stream of points on the X-Y plane. Design an algorithm that:</p>
<ul>
<li><strong>Adds</strong> new points from the stream into a data structure. <strong>Duplicate</strong> points are allowed and should be treated as different points.</li>
<li>Given a query point, <strong>counts</strong> the number of ways to choose three points from the data structure such that the three points and the query point form an <strong>axis-aligned square</strong> with <strong>positive area</strong>.</li>
</ul>
<p>An <strong>axis-aligned square</strong> is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.</p>
<p>Implement the <code>DetectSquares</code> class:</p>
<ul>
<li><code>DetectSquares()</code> Initializes the object with an empty data structure.</li>
<li><code>void add(int[] point)</code> Adds a new point <code>point = [x, y]</code> to the data structure.</li>
<li><code>int count(int[] point)</code> Counts the number of ways to form <strong>axis-aligned squares</strong> with point <code>point = [x, y]</code> as described above.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/09/01/image.png" alt="" /></p>
<p><strong>Input</strong> [“DetectSquares”, “add”, “add”, “add”, “count”, “count”, “add”, “count”] [ [], <a href="3,-10">3, 10</a>, <a href="11,-2">11, 2</a>, <a href="3,-2">3, 2</a>, <a href="11,-10">11, 10</a>, <a href="14,-8">14, 8</a>, <a href="11,-2">11, 2</a>, <a href="11,-10">11, 10</a>]</p>
<p><strong>Output:</strong> [null, null, null, null, 1, 0, null, 2]</p>
<p><strong>Explanation:</strong></p>
<p>DetectSquares detectSquares = new DetectSquares();</p>
<p>detectSquares.add([3, 10]);</p>
<p>detectSquares.add([11, 2]);</p>
<p>detectSquares.add([3, 2]);</p>
<p>detectSquares.count([11, 10]); // return 1. You can choose:
// - The first, second, and third points</p>
<p>detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure.</p>
<p>detectSquares.add([11, 2]); // Adding duplicate points is allowed.</p>
<p>detectSquares.count([11, 10]); // return 2. You can choose: // - The first, second, and third points // - The first, third, and fourth points</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>point.length == 2</code></li>
<li><code>0 <= x, y <= 1000</code></li>
<li>At most <code>3000</code> calls <strong>in total</strong> will be made to <code>add</code> and <code>count</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
DetectSquares
public DetectSquares()
-
-
Method Details
-
add
public void add(int[] point) -
count
public int count(int[] point)
-