- [Leetcode 3143. Maximum Points Inside the Square](#Leetcode 3143. Maximum Points Inside the Square)
- [1. 解题思路](#1. 解题思路)
- [2. 代码实现](#2. 代码实现)
 
1. 解题思路
这一题由于都是从中心开始的正方形,因此,我们只要找到所有tag下的点距离中心的位置最小的点,然后看有多少可以包括其中即可,我们可选的点必然是最近的点,次近的点的距离就是我们的边际,因此我们就可以定义出距离还有边界,看一共可以包括多少个不同tag的点。
2. 代码实现
给出python代码实现如下:
            
            
              python
              
              
            
          
          class Solution:
    def maxPointsInsideSquare(self, points: List[List[int]], s: str) -> int:
        dis = {}
        bound = math.inf
        for point, tag in zip(points, s):
            x, y = abs(point[0]), abs(point[1])
            d = max(x, y)
            if tag not in dis:
                dis[tag] = [d, 1]
            else:
                if dis[tag][0] > d:
                    bound = min(bound, dis[tag][0])
                    dis[tag] = [d, 1]
                elif dis[tag][0] == d:
                    dis[tag][1] += 1
                    bound = min(bound, d)
                else:
                    bound = min(bound, d)
        dis = sorted(dis.values(), key=lambda x: (x[0], -x[1]))
        ans = 0
        for d, cnt in dis:
            if d >= bound or cnt > 1:
                break
            ans += cnt
        return ans提交代码评测得到:耗时882ms,占用内存59.9MB。