2013. Detect Squares Medium

@problem@discussion
#Array#Hash Table#Design#Counting



1/**
2 * [2013] Detect Squares
3 *
4 * You are given a stream of points on the X-Y plane. Design an algorithm that:
5 * 
6 * 	Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as different points.
7 * 	Given a query point, counts the number of ways to choose three points from the data structure such that the three points and the query point form an axis-aligned square with positive area.
8 * 
9 * An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
10 * Implement the DetectSquares class:
11 * 
12 * 	DetectSquares() Initializes the object with an empty data structure.
13 * 	void add(int[] point) Adds a new point point = [x, y] to the data structure.
14 * 	int count(int[] point) Counts the number of ways to form axis-aligned squares with point point = [x, y] as described above.
15 * 
16 *  
17 * Example 1:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2021/09/01/image.png" style="width: 869px; height: 504px;" />
19 * Input
20 * ["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
21 * [[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
22 * Output
23 * [null, null, null, null, 1, 0, null, 2]
24 * Explanation
25 * DetectSquares detectSquares = new DetectSquares();
26 * detectSquares.add([3, 10]);
27 * detectSquares.add([11, 2]);
28 * detectSquares.add([3, 2]);
29 * detectSquares.count([11, 10]); // return 1. You can choose:
30 *                                //   - The first, second, and third points
31 * detectSquares.count([14, 8]);  // return 0. The query point cannot form a square with any points in the data structure.
32 * detectSquares.add([11, 2]);    // Adding duplicate points is allowed.
33 * detectSquares.count([11, 10]); // return 2. You can choose:
34 *                                //   - The first, second, and third points
35 *                                //   - The first, third, and fourth points
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	point.length == 2
41 * 	0 <= x, y <= 1000
42 * 	At most 3000 calls in total will be made to add and count.
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/detect-squares/
48// discuss: https://leetcode.com/problems/detect-squares/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52struct DetectSquares {
53        false
54    }
55
56
57/** 
58 * `&self` means the method takes an immutable reference.
59 * If you need a mutable reference, change it to `&mut self` instead.
60 */
61impl DetectSquares {
62
63    fn new() -> Self {
64        
65    }
66    
67    fn add(&self, point: Vec<i32>) {
68        
69    }
70    
71    fn count(&self, point: Vec<i32>) -> i32 {
72        
73    }
74}
75
76/**
77 * Your DetectSquares object will be instantiated and called as such:
78 * let obj = DetectSquares::new();
79 * obj.add(point);
80 * let ret_2: i32 = obj.count(point);
81 */
82
83// submission codes end
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_2013() {
91    }
92}
93


Back
© 2025 bowen.ge All Rights Reserved.