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 Macchiato1 天前
Leetcode 3568. Minimum Moves to Clean the Classroom
剪枝·广度优先遍历·leetcode medium·堆排·leetcode周赛452·leetcode 3568
Espresso Macchiato5 天前
Leetcode 3567. Minimum Absolute Difference in Sliding Submatrix
leetcode·leetcode medium·leetcode周赛452·leetcode 3567
Espresso Macchiato5 天前
Leetcode 3566. Partition Array into Two Equal Product Subsets
动态规划·leetcode medium·leetcode 3566·leetcode周赛452
Espresso Macchiato11 天前
Leetcode 3557. Find Maximum Number of Non Intersecting Substrings
动态规划·leetcode medium·leetcode 3557·leetcode双周赛157
Espresso Macchiato19 天前
Leetcode 3552. Grid Teleportation Traversal
广度优先遍历·leetcode medium·leetcode周赛450·leetcode 3552·堆排
Espresso Macchiato20 天前
Leetcode 3551. Minimum Swaps to Sort by Digit Sum
leetcode·排序·leetcode medium·leetcode 3551·leetcode周赛450
Espresso Macchiato23 天前
Leetcode 3543. Maximum Weighted K-Edge Path
leetcode·leetcode medium·图遍历·leetcode 3543·leetcode双周赛156
Espresso Macchiato1 个月前
Leetcode 3532. Path Existence Queries in a Graph I
leetcode medium·dsu·leetcode 3532·leetcode周赛447·uf
Espresso Macchiato1 个月前
Leetcode 3523. Make Array Non-decreasing
leetcode··leetcode medium·leetcode 3523·leetcode周赛446
Espresso Macchiato5 个月前
Leetcode 3418. Maximum Amount of Money Robot Can Earn
leetcode·动态规划·leetcode medium·leetcode 3418·leetcode周赛432