Leetcode 3143. Maximum Points Inside the Square

  • [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。

相关推荐
Espresso Macchiato2 天前
Leetcode 3781. Maximum Score After Binary Swaps
·leetcode medium·leetcode3781·leetcode双周赛172
Espresso Macchiato2 天前
Leetcode 3771. Total Score of Dungeon Runs
leetcode medium·leetcode 3771·leetocde周赛479
Espresso Macchiato2 天前
Leetcode 3790. Smallest All-Ones Multiple
leetcode medium·leetcode 3790·leetcode周赛482
Espresso Macchiato21 天前
Leetcode 3767. Maximize Points After Choosing K Tasks
leetcode medium·leetcode双周赛171·leetcode 3767
Espresso Macchiato21 天前
Leetcode 3765. Complete Prime Number
leetcode medium·leetcode双周赛171·leetcode 3765
Espresso Macchiato22 天前
Leetcode 3766. Minimum Operations to Make Binary Palindrome
leetcode medium·leetcode 3766·leetcode双周赛171
Espresso Macchiato2 个月前
Leetcode 3741. Minimum Distance Between Three Equal Elements II
滑动窗口·leetcode medium·leetcode 3741·leetcode周赛475
Espresso Macchiato3 个月前
Leetcode 3702. Longest Subsequence With Non-Zero Bitwise XOR
leetcode medium·异或操作·leetcode 3702·leetcode周赛470
Espresso Macchiato3 个月前
Leetcode 3694. Distinct Points Reachable After Substring Removal
滑动窗口·leetcode medium·leetcode双周赛166·leetcode 3694
Espresso Macchiato3 个月前
Leetcode 3698. Split Array With Minimum Difference
leetcode medium·分类讨论·leetcode周赛469·leetcode 3698