Leetcode 3027. Find the Number of Ways to Place People II

  • [Leetcode 3027. Find the Number of Ways to Place People II](#Leetcode 3027. Find the Number of Ways to Place People II)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题的话我也没想到啥特别好的思路,采用的纯粹是遍历+剪枝的思路。

遍历的话好理解,对于 N N N个位置当中要找到任意两个位置作为Takina和Chisato的位置,一共就是 O ( N 2 ) O(N^2) O(N2)的算法复杂度,然后就是要判断这两个位置是否合法,这个至多又会引入 O ( N ) O(N) O(N)的算法复杂度,一共可能就变成了 O ( N 3 ) O(N^3) O(N3)的算法复杂度,明显太多了......

因此,我们就是在这里做了一下剪枝,首先的话,就是我们将坐标拍了个序,按照题意要求,两个点一个要在左上角,一个要在右下角,因此,我们将坐标按照 ( x , − y ) (x, -y) (x,−y)进行逆序排列,此时必然左上角的点会出现右下角的点的前方,且如果他们的区间当中有其他点的话,这个点只能出现在他们之间。

此时,我们发现提交的代码就能够通过所有测试样例了,感觉应该还能够优化,不过这里暂时就没往下深挖了,凑合着就算是做出来了吧,LOL

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def numberOfPairs(self, points: List[List[int]]) -> int:
        points = sorted(points, key=lambda x: (x[0], -x[1]))
        n = len(points)
        ans = 0
        for i in range(n-1):
            a, b = points[i]
            for j in range(i+1, n):
                c, d = points[j]
                if b < d:
                    continue
                elif any(a <= e <= c and d <= f <= b for e, f in points[i+1:j]):
                    continue
                ans += 1
        return ans

提交代码评测得到:耗时6105ms,占用内存17MB。

相关推荐
源来猿往1 天前
yolov8n结构化剪枝
算法·yolo·剪枝
tongxianchao1 天前
MetaPruning: Meta Learning for Automatic Neural Network Channel Pruning
算法·机器学习·剪枝
deephub2 天前
DecEx-RAG:过程监督+智能剪枝,让大模型检索推理快6倍
人工智能·深度学习·大语言模型·agent·剪枝·reg
夜思红尘6 天前
算法--双指针
python·算法·剪枝
烟锁池塘柳06 天前
一文总结模型压缩技术:剪枝、量化与蒸馏的原理、实践与工程思考
算法·机器学习·剪枝
Fuly10248 天前
大模型剪枝(Pruning)技术简介
算法·机器学习·剪枝
Espresso Macchiato10 天前
Leetcode 3791. Number of Balanced Integers in a Range
leetcode hard·leetcode周赛482·leetcode 3791
Espresso Macchiato10 天前
Leetcode 3782. Last Remaining Integer After Alternating Deletion Operations
迭代·leetcode hard·leetcode双周赛172·leetcode 3782
星轨初途10 天前
郑州轻工业大学2025天梯赛解题
c++·经验分享·笔记·算法·链表·剪枝
Espresso Macchiato10 天前
Leetcode 3768. Minimum Inversion Count in Subarrays of Fixed Length
滑动窗口·leetcode hard·leetcode双周赛171·leetcode 3768