Skip to content

Latest commit

 

History

History
182 lines (147 loc) · 5.52 KB

File metadata and controls

182 lines (147 loc) · 5.52 KB

中文文档

Description

You are given a stream of points on the X-Y plane. Design an algorithm that:

  • Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as different points.
  • 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.

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.

Implement the DetectSquares class:

  • DetectSquares() Initializes the object with an empty data structure.
  • void add(int[] point) Adds a new point point = [x, y] to the data structure.
  • int count(int[] point) Counts the number of ways to form axis-aligned squares with point point = [x, y] as described above.

 

Example 1:

Input
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
Output
[null, null, null, null, 1, 0, null, 2]

Explanation DetectSquares detectSquares = new DetectSquares(); detectSquares.add([3, 10]); detectSquares.add([11, 2]); detectSquares.add([3, 2]); detectSquares.count([11, 10]); // return 1. You can choose: // - The first, second, and third points detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure. detectSquares.add([11, 2]); // Adding duplicate points is allowed. detectSquares.count([11, 10]); // return 2. You can choose: // - The first, second, and third points // - The first, third, and fourth points

 

Constraints:

  • point.length == 2
  • 0 <= x, y <= 1000
  • At most 3000 calls in total will be made to add and count.

Solutions

Python3

class DetectSquares:
    def __init__(self):
        self.mp = defaultdict(Counter)

    def add(self, point: List[int]) -> None:
        x, y = point
        self.mp[x][y] += 1

    def count(self, point: List[int]) -> int:
        x, y = point
        ans = 0
        if x not in self.mp:
            return ans
        xcnt = self.mp[x]

        for x1, counter in self.mp.items():
            if x1 != x:
                d = x1 - x
                ans += xcnt[y + d] * counter[y] * counter[y + d]
                ans += xcnt[y - d] * counter[y] * counter[y - d]
        return ans

Java

class DetectSquares {
    private Map<Integer, Map<Integer, Integer>> mp = new HashMap<>();

    public DetectSquares() {
    }

    public void add(int[] point) {
        int x = point[0], y = point[1];
        if (!mp.containsKey(x)) {
            mp.put(x, new HashMap<>());
        }
        mp.get(x).put(y, mp.get(x).getOrDefault(y, 0) + 1);
    }

    public int count(int[] point) {
        int x = point[0], y = point[1];
        int ans = 0;
        if (!mp.containsKey(x)) {
            return ans;
        }
        Map<Integer, Integer> xcnt = mp.get(x);
        for (Map.Entry<Integer, Map<Integer, Integer>> e : mp.entrySet()) {
            int x1 = e.getKey();
            Map<Integer, Integer> counter = e.getValue();
            if (x1 != x) {
                int d = x1 - x;
                ans += xcnt.getOrDefault(y + d, 0) * counter.getOrDefault(y, 0)
                    * counter.getOrDefault(y + d, 0);
                ans += xcnt.getOrDefault(y - d, 0) * counter.getOrDefault(y, 0)
                    * counter.getOrDefault(y - d, 0);
            }
        }
        return ans;
    }
}

/**
 * Your DetectSquares object will be instantiated and called as such:
 * DetectSquares obj = new DetectSquares();
 * obj.add(point);
 * int param_2 = obj.count(point);
 */

C++

class DetectSquares {
public:
    unordered_map<int, unordered_map<int, int>> mp;

    DetectSquares() {
    }

    void add(vector<int> point) {
        int x = point[0], y = point[1];
        ++mp[x][y];
    }

    int count(vector<int> point) {
        int x = point[0], y = point[1];
        int ans = 0;
        if (!mp.count(x)) return ans;
        auto xcnt = mp[x];
        for (auto e : mp) {
            int x1 = e.first;
            auto counter = e.second;
            if (x1 != x) {
                int d = x1 - x;
                ans += xcnt[y + d] * counter[y] * counter[y + d];
                ans += xcnt[y - d] * counter[y] * counter[y - d];
            }
        }
        return ans;
    }
};

/**
 * Your DetectSquares object will be instantiated and called as such:
 * DetectSquares* obj = new DetectSquares();
 * obj->add(point);
 * int param_2 = obj->count(point);
 */

...