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 Macchiato25 天前
Leetcode 3316. Find Maximum Removals From Source String
leetcode·动态规划·leetcode medium·leetcode 3316·leetcode双周赛141
Espresso Macchiato1 个月前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
Espresso Macchiato1 个月前
Leetcode 3301. Maximize the Total Height of Unique Towers
leetcode·leetcode medium·leetcode双周赛140·leetcode 3301
Espresso Macchiato1 个月前
Leetcode 3302. Find the Lexicographically Smallest Valid Sequence
leetcode medium·lcs·leetcode 3302·leetcode双周赛140·最大公共子序列
Espresso Macchiato3 个月前
Leetcode 3255. Find the Power of K-Size Subarrays II
leetcode·leetcode medium·leetcode 3255·leetcode 3254·leetcode周赛137
Espresso Macchiato3 个月前
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II
leetcode·leetcode medium·回文·leetcode 3240·leetcode双周赛136
Espresso Macchiato3 个月前
Leetcode 3234. Count the Number of Substrings With Dominant Ones
排列组合·leetcode medium·容斥原理·leetcode 3234·leetcode周赛408
Espresso Macchiato4 个月前
Leetcode 3201. Find the Maximum Length of Valid Subsequence I
leetcode medium·leetcode题解·leetcode 3201·leetcode周赛404
Espresso Macchiato4 个月前
Leetcode 3196. Maximize Total Cost of Alternating Subarrays
leetcode·动态规划·leetcode medium·leetcode周赛403·leetcode 3196
Espresso Macchiato4 个月前
Leetcode 3195. Find the Minimum Area to Cover All Ones I
leetcode·leetcode medium·leetcode题解·leetcode 3195·leetcode周赛403